Description
在无限大的二维平面的原点(0,0)放置着一个棋子。你有n条可用的移动指令,每条指令可以用一个二维整数向量表示。每条指令最多只能执行一次,但你可以随意更改它们的执行顺序。棋子可以重复经过同一个点,两条指令的方向向量也可能相同。你的目标是让棋子最终离原点的欧几里得距离最远,请问这个最远距离是多少?
Sample Input
5
2 -2
-2 -2
0 2
3 1
-3 1
Sample Output
26
按照极值排序之后肯定是连续的一段做为答案,
那我们假设当前枚举到点i,那么其实就只有处于同一个180度平面的点是有贡献的,你可以直接维护一个指针就行了。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL _max(LL x, LL y) {return x > y ? x : y;}
const double PI = acos(-1.0);
int read() {
int s = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
struct node {
int x, y;
double v;
} a[410000];
bool cmp(node a, node b) {return a.v < b.v;}
int main() {
int n = read();
for(int i = 1; i <= n; i++) a[i].x = read(), a[i].y = read(), a[i].v = atan2(a[i].y, a[i].x);
sort(a + 1, a + n + 1, cmp); int tp = 1;
LL sx = 0, sy = 0, ans = 0;
for(int i = 1; i <= n; i++) {
while(tp < i + n && a[tp].v - a[i].v < PI) sx += a[tp].x, sy += a[tp++].y, ans = _max(ans, sx * sx + sy * sy);
sx -= a[i].x, sy -= a[i].y, ans = _max(ans, sx * sx + sy * sy);
a[i + n] = a[i], a[i + n].v = a[i].v + 2.0 * PI;
} printf("%lld\n", ans);
return 0;
}