Description
在无限大的二维平面的原点(0,0)放置着一个棋子。你有n条可用的移动指令,每条指令可以用一个二维整数向量表
示。每条指令最多只能执行一次,但你可以随意更改它们的执行顺序。棋子可以重复经过同一个点,两条指令的方
向向量也可能相同。你的目标是让棋子最终离原点的欧几里得距离最远,请问这个最远距离是多少?
Input
第一行包含一个正整数n(n<=200000),表示指令条数。
接下来n行,每行两个整数x,y(|x|,|y|<=10000),表示你可以从(a,b)移动到(a+x,b+y)。
Output
输出一行一个整数,即最大距离的平方。
Sample Input
5
2 -2
-2 -2
0 2
3 1
-3 1
Sample Output
26
考虑最终的答案向量,如果知道方向了,显然我们就可以把
180
°
180°
180° 以内的向量全部选,那么只要把这个向量转一圈就可以了。
用双指针扫一下就能把所有可能的情况找出来了
复杂度
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define ll long long
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
// input a signed integer
template <class I>
inline void gi (I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
}
// print a signed integer
template <class I>
inline void print (I &x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
const double pi = acos(-1);
int n;
inline ll sqr(int x){return 1ll*x*x;}
struct point{int x,y;double k;}a[401000];
bool mycmp(point a,point b){
return a.k < b.k;
}
int main(){
gi(n); rep(i,1,n) gi(a[i].x),gi(a[i].y),a[i].k = atan2(a[i].y,a[i].x);
sort(a+1,a+n+1,mycmp);
rep(i,n+1,2*n) a[i] = a[i-n];
ll x = a[1].x,y = a[1].y,ans = sqr(x)+sqr(y);
int l = 1,r = 1;
rep(i,1,n){
while(r-l<n-1) if( atan2( a[r+1].y , a[r+1].x ) - atan2( a[l].y , a[l].x ) + (r+1>n?2.0*pi:0) <= pi ) {
r++;x += a[r].x;y += a[r].y;
ans = max( ans , sqr(x) + sqr(y) );
}else break;
x -= a[l].x;y -= a[l].y;l++;
ans = max( ans , sqr(x) + sqr(y) );
}
printf("%lld\n",ans);
return 0;
}