poj 2653 Pick-up sticks(几何线段相交判断)

时间:2023-02-10 18:15:55

Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10949   Accepted: 4085

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.poj 2653 Pick-up sticks(几何线段相交判断)

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.

Source

题意:按顺序给出一些木棍,输出在最上面的木棍标号

#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<vector>
#define N 100010
#define inf 99999999999.0

using namespace std;

struct Point {
double x,y;
} ;

struct Line {
Point p1,p2;
} L[N];

int n,num;
bool vis[N];

Point addPoint(double x,double y) {
Point it;
it.x=x;
it.y=y;
return it;
}

Line addLine(double x1,double y1,double x2,double y2) {
Point a=addPoint(x1,y1);
Point b=addPoint(x2,y2);
Line it;
it.p1=a,it.p2=b;
return it;
}

double multi(Point p0,Point p1,Point p2) {
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

///判断是否相交
bool is_inter(Point s1,Point e1,Point s2,Point e2) {
return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&
(max(s2.x,e2.x)>=min(s1.x,e1.x))&&
(max(s1.y,e1.y)>=min(s2.y,e2.y))&&
(max(s2.y,e2.y)>=min(s1.y,e1.y))&&
(multi(s1,s2,e1)*multi(s1,e1,e2)>0)&&
(multi(s2,s1,e2)*multi(s2,e2,e1)>0);
}

int ans[1010];

int main() {
//freopen("test.in","r",stdin);
while(cin>>n&&n) {
double x,y,_x,_y;
num=0;
memset(vis,0,sizeof vis);
for(int i=0; i<n; i++) {
scanf("%lf%lf%lf%lf",&x,&y,&_x,&_y);
L[i]=addLine(x,y,_x,_y);
}
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if(is_inter(L[i].p1,L[i].p2,L[j].p1,L[j].p2)) {
vis[i]=1;
break;
}
}
}
int k=0;
for(int i=0; i<n; i++) {
if(!vis[i]) {
ans[k++]=i+1;
}
}
printf("Top sticks: ");
for(int i=0; i<k-1; i++)
printf("%d, ",ans[i]);
printf("%d.\n",ans[k-1]);
}
return 0;
}