附上原题链接:https://codeforces.com/contest/996/problem/E
这题的解法真的很玄学,简单来说就是对这个数组进行一种操作,如果该操作的结果不能满足条件,就对该数组进行随机排列,再进行这种操作,重复上述过程,知道满足条件。
#include<bits/stdc++.h>
#include<iostream>
#include<sstream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
typedef long long ll;
const ll N = 1e5+10;
const ll L = 1.5e6 * 1.5e6;
struct node
{
int x, y, id;
};
node a[N];
int ans[N];
int n;
int main()
{
cin>>n;
for(int i=0; i<n; i++)
{
cin >> a[i].x >> a[i].y;
a[i].id = i;
}
while(1)
{
ll X = 0, Y = 0;
for(int i=0; i<n; i++)
{
if( (X+a[i].x)*(X+a[i].x) + (Y+a[i].y)*(Y+a[i].y) <= (X-a[i].x)*(X-a[i].x) + (Y-a[i].y)*(Y-a[i].y) )
{
ans[ a[i].id ] = 1;
X = X + a[i].x;
Y = Y + a[i].y;
}
else
{
ans[a[i].id ] = -1;
X = X - a[i].x;
Y = Y - a[i].y;
}
}
if(X*X + Y*Y <= L)
{
//cout << sqrt(X*X + Y*Y) <<endl;
break;
}
random_shuffle(a, a+n);
}
for(int i=0; i<n; i++)
{
cout << ans[i] << ' ';
}
//system("pause");
return 0;
}