Pick-up sticks(线段相交判断+剪枝)
来源:互联网 发布:爱奇艺显示网络未连接 编辑:程序博客网 时间:2024/05/16 01:31
Link:http://poj.org/problem?id=2653
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 11050 Accepted: 4120
Description
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.
Input
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.
Output
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.
The picture to the right below illustrates the first case from input.
Sample Input
51 1 4 22 3 3 11 -2.0 8 41 4 8 23 3 6 -2.030 0 1 11 0 2 12 0 3 10
Sample Output
Top sticks: 2, 4, 5.Top sticks: 1, 2, 3.
Hint
Huge input,scanf is recommended.
Source
Waterloo local 2005.09.17
AC code:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#include<set>#include<map>#define EPS 1e-8using namespace std;#define MIN(x,y) (x < y ? x : y)#define MAX(x,y) (x > y ? x : y) typedef struct { double x,y;} Point;struct line{Point s;Point e;}seg[100010];bool deleted[100010];int ans[100010];double multi(Point p1,Point p2,Point p0){ return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}int Acoross(struct line v1,struct line v2){ if(max(v1.s.x,v1.e.x)>=min(v2.s.x,v2.e.x)&& max(v2.s.x,v2.e.x)>=min(v1.s.x,v1.e.x)&& max(v1.s.y,v1.e.y)>=min(v2.s.y,v2.e.y)&& multi(v2.s,v1.e,v1.s)*multi(v1.e,v2.e,v1.s)>=0&& multi(v1.s,v2.e,v2.s)*multi(v2.e,v1.e,v2.s)>=0) return 1;//相交 return 0;//不相交}int main(){ //freopen("D:\in.txt","r",stdin); int n,i,j,k,fg,t,cnt;while(scanf("%d",&n)!=EOF){if(n==0)break;for(i=1;i<=n;i++){scanf("%lf%lf%lf%lf",&seg[i].s.x,&seg[i].s.y,&seg[i].e.x,&seg[i].e.y);}memset(deleted,false,sizeof(deleted));for(i=1;i<n;i++){for(j=i+1;j<=n;j++){if(Acoross(seg[i],seg[j])){deleted[i]=true;//printf("%d %d\n",i,j);break;//剪枝,不然TLE!!! }}}printf("Top sticks:");cnt=0;for(i=1;i<=n;i++){if(!deleted[i]){ans[++cnt]=i;}}for(i=1;i<cnt;i++)printf(" %d,",ans[i]);printf(" %d.\n",ans[cnt]);}return 0;}
0 0
- Pick-up sticks(线段相交判断+剪枝)
- Pick-up sticks(线段相交判断)
- POJ2653 Pick-up sticks 判断线段相交
- poj 2653 Pick-up sticks(判断线段是否相交)
- poj 2653 Pick-up sticks(判断线段是否相交)
- POJ 2653 Pick-up sticks (判断线段相交)
- POJ2653 Pick-up sticks(线段相交判断)
- hdu acm 1147 Pick-up sticks(判断线段相交)
- hdu-1147 Pick-up sticks(判断线段相交)
- Pick-up sticks(判断线段是否相交)
- POJ 2653 Pick-up sticks(判断线段相交)
- poj 2653 Pick-up sticks(判断线段相交)
- POJ 2653 Pick-up sticks(判断线段相交)
- poj2653 Pick-up sticks(判断线段相交)
- hdu_1147 Pick-up sticks(线段相交)
- HDU1147:Pick-up sticks(线段相交)
- POJ 2653 Pick-up sticks(判断线段相交)
- POJ 2653 Pick-up sticks 枚举判断线段是否相交
- Android WIFI 开发
- Jlink SWD和Jtag下载失败总结
- HDOJ 题目3518 Boring counting(后缀数组,求不重叠重复次数最少为2的子串种类数)
- Android自定义notification实现进度条功能
- c unistd.h说明
- Pick-up sticks(线段相交判断+剪枝)
- JDK的安装以及配置
- POJ 1328 Radar Installation
- C++ 学习博客链接
- 黑马程序员——黑苹果的日记(7)——Foundation(2)
- hibernate向mysql插入中文乱码问题
- 33
- 怎样布局网站关键词最合理
- POJ 1789 解题报告