Pick-up sticks
Time Limit: 6000/3000 ms (Java/Other) Memory Limit: 131072/65536 K (Java/Other)
Total Submission (s): 5 Accepted Submission (s): 5
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 instances. 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 shoshould 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 shocould be listed in order in which they were thrown.
The picture to the right below without strates 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
PKU
[Cpp]
// 12-8-21.cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
# Include <cstdio>
# Include <iostream>
Using namespace std;
# Define eps-1e-6
# Deprecision MAX 100005
Struct Point
{
Double x, y;
};
Struct Line
{
Point a, B;
Int flag;
} S [MAX];
Inline double cross (Point & o, Point & a, Point & B) // oa * ob (. x-o.x,. y-o.y) * (B. x-o.x, B. y-o.y) * indicates the Cross Product
{
Return (a. x-o.x) * (B. y-o.y)-(a. y-o.y) * (B. x-o.x );
}
Inline bool quick (Line & p, Line & q)
{// A fast exclusion, a cross-vertical, and the intersection of line segments
If (min (p. a. x, p. B. x) <= max (q. a. x, q. B. x )&&
Min (q. a. x, q. B. x) <= max (p. a. x, p. B. x )&&
Min (p. a. y, p. B. y) <= max (q. a. y, q. B. y )&&
Min (q. a. y, q. B. y) <= max (p. a. y, p. B. y )&&
Cross (p. a, q. a, q. b) * cross (p. b, q. a, q. b) <eps & // verify whether the vertices at both ends of p are at both ends of q
Cross (q. a, p. a, p. b) * cross (q. b, p. a, p. b) <eps) // verify whether the vertices at both ends of q are at both ends of p.
Return true;
Return false;
}
Int main ()
{
Int n, I, j;
While (scanf ("% d", & n), n)
{
For (I = 1; I <= n; I ++)
{
Scanf ("% lf", & s [I]. a. x, & s [I]. a. y, & s [I]. b. x, & s [I]. b. y );
S [I]. flag = 1;
}
Printf ("Top sticks :");
For (I = 1; I <n; I ++)
{
For (j = I; j <= n; j ++)
If (quick (s [I], s [j]) break;
If (j = n + 1) printf ("% d,", I );
}
Printf ("% d. \ n", n );
}
Return 0;
}