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 p−→vip−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.5⋅106|p|≤1.5⋅106 so that he can stay safe.

Input

The first line contains a single integer nn (1≤n≤1051≤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=1ci→vip=∑i=1ncivi→, satisfies |p|≤1.5⋅106|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=;
const ll p=;
ll ans[maxn+],x[maxn+],y[maxn+];
ll X,Y,P;
int main()
{
int N,i,j; P=p*p;scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%I64d%I64d",&x[i],&y[i]);
if(X>=&&Y>=&&x[i]<=&&y[i]<=) X+=x[i],Y+=y[i],ans[i]=;
else if(X>=&&Y>=&&x[i]>=&&y[i]>=) X-=x[i],Y-=y[i],ans[i]=-;
else if(X<=&&Y<=&&x[i]<=&&y[i]<=) X-=x[i],Y-=y[i],ans[i]=-;
else if(X<=&&Y<=&&x[i]>=&&y[i]>=) X+=x[i],Y+=y[i],ans[i]=;
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]=;
else X-=x[i],Y-=y[i],ans[i]=-;
}
for(i=;i<=N;i++) printf("%d ",ans[i]);
return ;
}

AC代码:

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

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

CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)的更多相关文章

  1. Codeforces 996E Leaving the Bar (随机化)

    题目连接:Leaving the Bar 题意:给你n个向量,你可以加这个向量或减这个向量,使得这些向量之和的长度小于1.5e6. 题解: 按照正常的贪心方法,最后的结果有可能大于1.5e6 .这里我 ...

  2. 浅浅地谈一下随机算法【poj2454】【poj3318】

    随机算法我也只是稍微接触了一下,就是想写篇博客自己稍微总结一下 其实随机算法也算是一个玄学吧,运气不好还是会wa.但是我们知道,计算机可以在短时间内计算大量的数据,所以碰到正确答案的概率还是挺大的. ...

  3. 微信红包中使用的技术:AA收款+随机算法

    除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...

  4. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  5. 抽奖随机算法的技术探讨与C#实现

    一.模拟客户需求 1.1 客户A需求:要求每次都按照下图的概率随机,数量不限,每个用户只能抽一次,抽奖结果的分布与抽奖概率近似. 1.2 客户B需求:固定奖项10个,抽奖次数不限,每个用户只能抽一次, ...

  6. hdu 4712 (随机算法)

    第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...

  7. 权重随机算法的java实现

    一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果 ...

  8. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. HDU4712+随机算法

    随机算法 求n个20位的2进制串的MinDist. Dist:两个串的异或结果中1的个数 /* 随机算法 */ #include<algorithm> #include<iostre ...

随机推荐

  1. Ant Problem: failed to create task or type foreach 问题

    用eclipse导出android时总是会出现有类没有导出的现象,感觉非常麻烦,就用ant些了脚本.在eclipse中运行脚本没问题.可是在命令行下运行会出现 Problem: failed to c ...

  2. 双十一前4小时,CentOS 6.5server启动错误排查

    11月10日晚上8点多.眼看要到双十一了... 但我要说的这段经历却和双十一毫无关系.哈哈. 这天准备向CentOS6.5server的svn上传一些文件,结果开机启动时,却出现了以下的界面: 这是肿 ...

  3. Android 虚化图片的方法

    Android 虚化图片 模糊图片 图片毛玻璃效果. 效果如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaDNjNGxlbm92bw==/font/ ...

  4. 仿VS安装界面小球滑动效果

    在Visual Studio 2010后续版本的安装界面中,可以发现一组小球在滑动表示安装程序正在进行: 于是尝试用CSS实现了一下. 首先需要建立用来表示小球的html结构: <div cla ...

  5. emacs 简记

    简介 Emacs作为神的编辑器,不用介绍了吧,说点感受. 用了一段时间了,总体感觉其实Emacs是很简单的,甚至比vim还简单,因为在X环境下,打开后可以就像记事本一样使用.但是,使用Emacs的人一 ...

  6. Getting Started with the G1 Garbage Collector(译)

    原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...

  7. Linux 批量替换的一种实现方式

    替换某目录下所有文件中的某个字符: sed -i 's/origin_str/new_str/g' `grep origin_str -rl ./` origin_str:被替换的字符串: new_s ...

  8. JS基础常识理解

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Java 学习 day06

    01-面向对象(Static关键字) package myFirstCode; /* 静态:static. 用法:是一个修饰符,用于修饰成员(成员变量,成员函数) 当成员被静态修饰后,就多了一个调用方 ...

  10. Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.

    Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...