Pick-up sticks
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2293 Accepted Submission(s): 842
Problem 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.
Source
University of Waterloo Local Contest 2005.09.17
题目分析:
枚举当亲每条直线,然后枚举之前没有覆盖掉的直线,覆盖掉的直线去掉,没有覆盖的保留,最终保留的就是最后最后结果,用一个数组模拟记录即可,姿势很漂亮
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define MAX 100007
#define N 1007
#define eps 1e-10
using namespace std;
int n,cc;
struct Point
{
double x , y;
Point ():x(0),y(0){}
Point ( double a , double b )
:x(a),y(b){}
};
struct Line
{
Point u,v;
}l[MAX];
int sig ( double d )
{
return fabs(d)<eps?0:d<0?-1:1;
}
double cross ( Point& o , Point& a , Point& b )
{
return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y);
}
bool OnSegment ( Point& a , Point& b , Point& c )
{
return c.x >= min ( a.x , b.x ) && c.x <= max ( a.x , b.x )
&& c.y >= min ( a.y , b.y ) && c.y <= max ( a.y , b.y );
}
bool segCross ( Point& a , Point& b , Point& c , Point& d )
{
double s1,s2;
int d1 , d2 , d3 , d4;
d1 = sig ( s1 = cross ( a , b , c ) );
d2 = sig ( s2 = cross ( a , b , d ) );
d3 = sig ( cross ( c , d , a ));
d4 = sig ( cross ( c , d , b ));
if ( (d1^d2) == -2 && (d3^d4) == -2 ) return 1;
else if ( d1 == 0 && OnSegment ( a , b , c ) ) return 1;
else if ( d2 == 0 && OnSegment ( a , b , d ) ) return 1;
else if ( d3 == 0 && OnSegment ( c , d , a ) ) return 1;
else if ( d4 == 0 && OnSegment ( c , d , a ) ) return 1;
return 0;
}
int single[N];
int main ( )
{
while ( ~scanf ( "%d" , &n ) , n )
{
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%lf%lf%lf%lf" , &l[i].u.x , &l[i].u.y , &l[i].v.x , &l[i].v.y );
int p ,q;
p = 0;
for ( int i = 1 ; i <= n ; i++ )
{
q = 0;
for ( int j = 0 ; j < p ; j++ )
if ( !segCross ( l[i].u , l[i].v , l[single[j]].u , l[single[j]].v ) )
single[q++] = single[j];
single[q++] = i;
p = q;
}
printf ( "Top sticks: %d" , single[0] );
for ( int i = 1 ; i < p ; i++ )
printf ( ", %d" , single[i] );
puts ( "." );
}
}