AcWing 1237. 螺旋折线
原题链接
中等
作者:
Lemmon_kk
,
2021-04-06 11:23:38
,
所有人可见
,
阅读 489
leetcode 春季赛遇到了
来补一发题解
思路:
计算内侧正方形有多少个
cnt = max(abs(xPos), abs(yPos)) - 1;
cnt = max(0ll, cnt);
运用等差数列求和
outside = t * 2;
inside = 2;
如果inside > outside: now = 0
然后计算当前边长
util = t + 1 << 1
分四种情况去特判
#include <iostream>
using namespace std;
typedef long long LL;
LL xPos, yPos;
LL now;
int main(){
cin >> xPos >> yPos;
LL t = max(abs(xPos), abs(yPos)) - 1;
t = max(0ll, t);
LL outside = (t * 2);
LL inside = 2;
if(outside >= inside) now = (outside + inside) * t / 2ll * 4;
LL util = (t + 1) << 1;
if(xPos == t + 1){
now += util * 2 + abs(yPos - (t + 1));
}else if(xPos == -(t + 1)){
now += abs(yPos + (t + 1));
}else if(yPos == t + 1){
now += util + abs(xPos + (t + 1));
}else if(yPos == -(t + 1)){
now += util * 3 + abs(xPos - (t + 1));
}
cout << now << endl;
return 0;
}
腻害
媳妇儿来查房了
我就来看个题解。。。