您的位置:首页 > 其它

poj 2653 Pick-up sticks 计算几何

2016-07-26 09:49 525 查看
poj 2653 Pick-up sticks 计算几何

Pick-up sticks

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 12401 Accepted: 4677
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.


Sample Input
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

Sample Output
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

题意:按先后顺序给你n条线段,判断哪些线段在最上面

思路:先按顺序存入所有线段记为1到n,因为最后一条线段一定在最上面,所以我们可以从第n条扫到第1条,记为i,然后从第n条一直到第i+1条线段判断是否与它相交,如果是,那么第i条线段一定不在最上面,否则一定在最上面

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct Point //点
{
double x,y;
Point (double a=0,double b=0):x(a),y(b) {}
};

struct Line_segment //线段
{
Point s,e;
Line_segment() {}
Line_segment(Point a,Point b):s(a),e(b) {}
} a[100001];

inline double Max(double a,double b)
{
return a>b?a:b;
}

inline double Min(double a,double b)
{
return a<b?a:b;
}
double multiply(Point sp,Point  ep,Point op)
{
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}

bool intersect(Line_segment u,Line_segment v)
{
return((Max(u.s.x,u.e.x)>=Min(v.s.x,v.e.x))&&
(Max(v.s.x,v.e.x)>=Min(u.s.x,u.e.x))&&
(Max(u.s.y,u.e.y)>=Min(v.s.y,v.e.y))&&
(Max(v.s.y,v.e.y)>=Min(u.s.y,u.e.y))&&
(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&&
(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));
}

bool jug[100001];
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
if(n==0) break;
for(i=0; i<n; i++)
jug[i]=1;
for(i=0; i<n; i++)
scanf("%lf %lf %lf %lf",&a[i].e.x,&a[i].e.y,&a[i].s.x,&a[i].s.y);
for(i=n-1; i>=0; i--)
{
for(j=i+1; j<n; j++)
if(intersect(a[i],a[j])==1)
{
jug[i]=0;
break;
}
}
printf("Top sticks: ");
for(i=0; i<n; i++)
if(jug[i]==1)
{
printf("%d",i+1);
i++;
break;
}
for(; i<n; i++)
if(jug[i]==1) printf(", %d",i+1);
printf(".\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ 数学 计算几何