hdu acm 1147 Pick-up sticks(判断线段相交)
2016-02-18 10:15
302 查看
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2627 Accepted Submission(s): 954
[align=left]Problem Description[/align]
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has
noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
![](http://acm.hdu.edu.cn/data/images/1147-1.jpg)
[align=left]Input[/align]
Input consists of a number of cases. The data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints
of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
[align=left]Output[/align]
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
[align=left]Sample Input[/align]
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
[align=left]Sample Output[/align]
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
题意:棍子从第1根放到第n根,求最后在最上面的棍子的编号。
Total Submission(s): 2627 Accepted Submission(s): 954
[align=left]Problem Description[/align]
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has
noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
![](http://acm.hdu.edu.cn/data/images/1147-1.jpg)
[align=left]Input[/align]
Input consists of a number of cases. The data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints
of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
[align=left]Output[/align]
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
[align=left]Sample Input[/align]
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
[align=left]Sample Output[/align]
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
题意:棍子从第1根放到第n根,求最后在最上面的棍子的编号。
#include<iostream> #include<stdio.h> #include<queue> #include<string.h> #include<algorithm> #define maxn 1000005 #define MS(a,b) memset(a,b,sizeof(a)) using namespace std; struct Point { double x,y; }; double mult(Point a,Point b,Point c)//叉积。 { return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } bool intersect(Point aa,Point bb,Point cc,Point dd)//判断线段相交。 { if(max(aa.x,bb.x)<min(cc.x,dd.x)) return false; if(max(aa.y,bb.y)<min(cc.y,dd.y)) return false; if(max(cc.x,dd.x)<min(aa.x,bb.x)) return false; if(max(cc.y,dd.y)<min(aa.y,bb.y)) return false; if(mult(cc,bb,aa)*mult(bb,dd,aa)<0) return false; if(mult(aa,dd,cc)*mult(dd,bb,cc)<0) return false; return true; } Point s[maxn],e[maxn]; int vis[100005],ans[100005]; int main() { int n,i,j; while(scanf("%d",&n)!=EOF) { if(n==0)break; MS(vis,0); for(i=1;i<=n;i++) scanf("%lf %lf %lf %lf",&s[i].x,&s[i].y,&e[i].x,&e[i].y); for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(intersect(s[i],e[i],s[j],e[j])) { vis[i]=1;//如果两条棍子相交就把编号小的那根覆盖掉。 } if(vis[i])break; } } printf("Top sticks:"); j=0; for(i=1;i<=n;i++) if(!vis[i]) ans[j++]=i; for(i=0;i<j-1;i++) printf(" %d,",ans[i]); printf(" %d.\n",ans[i]); } return 0; }
相关文章推荐
- Python 实用技巧
- 在iOS系统上设计用户登录(原创技巧)
- Android在Eclipse上进行开发时分包
- 笔记6,使用jQuery操作DOM
- MongoDB是?
- 转 Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)
- Handler本质简析与使用实例
- ASP.NET 执行bat文件。
- 软件发布版本区别介绍-Alpha,Beta,RC,Release
- 如何让网页显示友好的错误信息页面
- 为什么Java 两个Integer 中1000==1000为false而100==100为true?
- Andorid中.9.png图片的使用及制作
- <大型网站技术架构>读书笔记
- 博客园第三方客户端-i博客园正式发布App Store
- zoj 3627 贪心模拟
- 分类 继承 封装 多态
- java虚拟机加载系统环境变量到内存中
- 一言之思-3
- lsof 常用命令
- 遥感图像滤波处理