Live2d Test Env

CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)

For a vector v=(x,y)v→=(x,y), define |v|=x2+y2|v|=x2+y2.

Allen had a bit too much to drink at the bar, which is at the origin. There are nn vectors v1,v2,,vnv1→,v2→,⋯,vn→. Allen will make nn moves. As Allen's sense of direction is impaired, during the ii-th move he will either move in the direction vivi→ or vi−vi→. In other words, if his position is currently p=(x,y)p=(x,y), he will either move to p+vip+vi→ or pvip−vi→.

Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position pp satisfies |p|1.5106|p|≤1.5⋅106 so that he can stay safe.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of moves.

Each of the following lines contains two space-separated integers xixi and yiyi, meaning that vi=(xi,yi)vi→=(xi,yi). We have that |vi|106|vi|≤106 for all ii.

Output

Output a single line containing nn integers c1,c2,,cnc1,c2,⋯,cn, each of which is either 11 or 1−1. Your solution is correct if the value of p=ni=1civip=∑i=1ncivi→, satisfies |p|1.5106|p|≤1.5⋅106.

It can be shown that a solution always exists under the given constraints.

Examples
input
Copy
3
999999 0
0 999999
999999 0
output
Copy
1 1 -1 
input
Copy
1
-824590 246031
output
Copy
1 
input
Copy
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
output
Copy
1 1 1 1 1 1 1 -1 

题意:给定一些向量,你可以改变它的符号,使得这些向量之和的长度小于1.5e6。

思路:考虑到每条边的长度小于1e6,所以任意两条边的向量和长度小于1.414e6<1.5e6。任意三条边的向量和(可以改变方向)的长度也成立,下面给出证明...

所以我们贪心, 保证向量和离原点更近. 然后下面的代码AC了,然后又被FST了.是因为每次我贪心原则是一样的.最后的结果有可能大于1.5e6.  我们需要加一些随机性, 多次贪心,直到结果满足题意.(这里随机的作用就算多次贪心,直到满足

证明:每三个向量abc中都能找到两个向量合起来 <= 1e6,然后不断合并,最后只会剩下一个或者两个向量。

对于a和b,1,若ab小于60度,其中一个转向,则变为大于120度,<1e6;2,ab角度大于120度,则其向量和的长度小于1e6。3,角度在60到120度之间,那么第三边c,不论怎么放,都或与a或与b满足上面的关系,使得其向量和长度小于1e6,那么一直这样合并,最后只剩下一边或者两边,而两边的情况最坏为1.414e6

FST代码,WA79:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000000;
const ll p=1500000;
ll ans[maxn+10],x[maxn+10],y[maxn+10];
ll X,Y,P;
int main()
{
    int N,i,j; P=p*p;scanf("%d",&N);
    for(i=1;i<=N;i++){
       scanf("%I64d%I64d",&x[i],&y[i]);
       if(X>=0&&Y>=0&&x[i]<=0&&y[i]<=0) X+=x[i],Y+=y[i],ans[i]=1;
       else if(X>=0&&Y>=0&&x[i]>=0&&y[i]>=0) X-=x[i],Y-=y[i],ans[i]=-1;
       else if(X<=0&&Y<=0&&x[i]<=0&&y[i]<=0) X-=x[i],Y-=y[i],ans[i]=-1;
       else if(X<=0&&Y<=0&&x[i]>=0&&y[i]>=0) X+=x[i],Y+=y[i],ans[i]=1;
       else if((X+x[i])*(X+x[i])+(Y+y[i])*(Y+y[i])<(X-x[i])*(X-x[i])+(Y-y[i])*(Y-y[i])) X+=x[i],Y+=y[i],ans[i]=1;
       else X-=x[i],Y-=y[i],ans[i]=-1;
    }
    for(i=1;i<=N;i++) printf("%d ",ans[i]);
    return 0;
}

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000000;
const ll p=1500000;
ll X,Y,P; int ans[maxn];
struct in{ ll x,y,id; }s[maxn];
int main()
{
    int N,i,j; P=p*p;scanf("%d",&N);
    for(i=1;i<=N;i++){
       scanf("%I64d%I64d",&s[i].x,&s[i].y); s[i].id=i;
    }
    while(true){
       random_shuffle(s+1,s+N+1); X=Y=0;
       for(i=1;i<=N;i++){        
          if((X+s[i].x)*(X+s[i].x)+(Y+s[i].y)*(Y+s[i].y)>(X-s[i].x)*(X-s[i].x)+(Y-s[i].y)*(Y-s[i].y)) X-=s[i].x,Y-=s[i].y,ans[s[i].id]=-1;
          else X+=s[i].x,Y+=s[i].y,ans[s[i].id]=1;
       }
       if(X*X+Y*Y<=P) {
               for(i=1;i<=N;i++) printf("%d ",ans[i]); return 0;
       }
    }
    return 0;
}

 

 

 (div1E也是需要随机算法!!!

 

posted @ 2018-06-25 16:36  nimphy  阅读(600)  评论(0编辑  收藏  举报