AcWing 1237. 螺旋折线
原题链接
中等
作者:
你若安好便是晴天
,
2021-02-09 14:52:24
,
所有人可见
,
阅读 354
题目描述
样例
算法1
(暴力枚举) $O(n^2)$
时间复杂度
参考文献
C++ 代码
算法2
(暴力枚举) $O(n^2)$
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
LL x,y;
LL res=0;
LL ax,ay;
int main(){
cin>>x>>y;
ax=abs(x);
ay=abs(y);
LL mxy=max(ax,ay);
LL count1=((mxy-1)*2+1)*((mxy-1)*2+1);
LL startx=-mxy,starty=-(mxy-1);
LL count2;
if(x==startx){
cout<<count1+y-starty+1-1;
}
else if(y==mxy){
count2=mxy-starty+1;
count2=count2+(x-startx);
cout<<count1+count2-1;
}
else if(x==mxy){
count2=mxy-starty+1+(mxy-startx);
count2=count2+abs(y-mxy);
cout<<count1+count2-1;
}
else{
count2=mxy-starty+1+(mxy-startx)+2*mxy;
count2=count2+abs(x-mxy);
cout<<count1+count2-1;
}
return 0;
}