欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 2653 Pick-up sticks

程序员文章站 2022-03-12 09:50:26
...

POJ 2653 Pick-up sticks

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.

这道题因为有3秒时限所以就试了一下暴力,然后就过了……数据水了,就是用线段交判断每一条线段上有没有线段与其相交即可:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+5;

bool flag[N];
struct point{
double x,y;
point(double a=0,double b=0) {x=a; y=b;}
};

struct segment
{
 point s;
 point e;
segment(point a,point b) { s=a; e=b;}
segment() { }
}s[N];

double cross(point a,point b,point c){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}

bool intersect(segment u,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))&&
   (cross(v.s,u.e,u.s)*cross(u.e,v.e,u.s)>=0)&&         //跨立实验
   (cross(u.s,v.e,v.s)*cross(v.e,u.e,v.s)>=0));
}

int main()
{
    int n;
    while(~scanf("%d",&n) && n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf%lf",&s[i].s.x,&s[i].s.y,&s[i].e.x,&s[i].e.y);
            flag[i]=1;
        }
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(intersect(s[i],s[j])) {flag[i]=0;break;}
            }
        }
        printf("Top sticks:");
        for(int i=0;i<n;i++){
            if(i==n-1)  printf(" %d.",i+1);
            else if(flag[i]) printf(" %d,",i+1);
        }
        puts("");
    }
    return 0;
}