您的位置:首页 > 其它

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.



 

[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: