Pick-up sticks

来源:互联网 发布:羽西的淘宝旗舰店 编辑:程序博客网 时间:2024/06/04 19:05

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
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.


#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<math.h>#include<queue>#include<iomanip>//#include<bits/stdc++.h>#define eps 1e-8#define inf 0x7f//const double inf=100;using namespace std;int v[110000];struct Line{    double x1,y1,x2,y2;}line[110000];double cross(int x1,int y1,int x2,int y2,int x,int y){    return(x2-x1)*(y-y1)-(x-x1)*(y2-y1);}bool quick_check(Line a,Line b){    if(min(a.x1,a.x2)<=max(b.x1,b.x2)&&min(a.y1,a.y2)<=max(b.y1,b.y2)&&min(b.x1,b.x2)<=max(a.x1,a.x2))      return 1;     return 0;}bool kua_li(Line a,Line b){    if(!quick_check(a,b)) return 0;    if(cross(a.x1,a.y1,a.x2,a.y2,b.x1,b.y1)*cross(a.x1,a.y1,a.x2,a.y2,b.x2,b.y2)>=-eps) return 0;    if(cross(b.x1,b.y1,b.x2,b.y2,a.x1,a.y1)*cross(b.x1,b.y1,b.x2,b.y2,a.x2,a.y2)>=-eps) return 0;    return 1;}int main(){    int n;    while(~scanf("%d",&n)&&n)    {        memset(v,0,sizeof(v));        for(int i=0;i<n;i++)            scanf("%lf%lf%lf%lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);       for(int i=0;i<n;i++)       {           for(int j=i+1;j<n;j++)           {               if(kua_li(line[i],line[j]))               {                   v[i]=1;                   break;               }           }       }        printf("Top sticks: ");        int p=0;        for(int i=0;i<n;i++)        {            if(v[i]==0)            {                if(p==0)                {                    printf("%d",i+1);                    p++;                }                else                    printf(", %d",i+1);            }        }        printf(".\n");      }}



原创粉丝点击