Pick-up sticks
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3114 Accepted Submission(s): 1170
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.
![](http://acm.hdu.edu.cn/data/images/1147-1.jpg)
![](http://acm.hdu.edu.cn/data/images/1147-1.jpg)
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.
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
题意:给n跟筷子,按顺序放到地上,输出最后在最上面的筷子编号。
思路:每根筷子做一次线段相交判断即可。
# include <iostream> # include <cmath> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 1e5; struct node { double x, y; }a[maxn+3], b[maxn+3]; int n, m, c[maxn+3]; double cha(node a, node b, node c)//向量叉乘。 { return a.x*b.y + b.x*c.y + c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y; } bool fun(node a, node b, node c, node d) { if((max(a.x, b.x)>=min(c.x, d.x) || min(a.x, b.x)<=max(c.x, d.x)) && (max(a.y, b.y)>=min(c.y, d.y) || min(a.y, b.y)<=max(c.y, d.y)))//快速排斥。 { if(cha(a, b, c)*cha(a, b, d) <= 0 && cha(c, d, a)*cha(c, d, b)<=0) return true; else return false; } else return false; } int main() { while(~scanf("%d",&n),n) { m = n; memset(c, 0, sizeof(c)); for(int i=1; i<=n; ++i) scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y); for(int i=1; i<=n; ++i) for(int j=i+1; j<=n; ++j) if(fun(a[i], b[i], a[j], b[j])) { c[i] = 1; --m; break; } printf("Top sticks: "); for(int i=1; i<=n; ++i) if(!c[i]&&m) { --m; printf("%d%c%c",i,!m?'.':',', !m?'\n':' '); } } return 0; }