Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11043 Accepted: 4119 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 0Sample Output
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.Hint
Huge input,scanf is recommended.
Source
这个很简单~模板大法好~
题意:多实例测试,每组n个线段,0结束,判断所有的线段中没有被后放的线段所压着的线段有多少,输出它们的编号(从1开始),
输出的格式详见样例~
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 #include <math.h> 7 #include <algorithm> 8 #include <cctype> 9 #include <string> 10 #include <map> 11 #include <set> 12 #define ll long long 13 using namespace std; 14 15 const double eps = 1e-8; 16 int sgn(double x) 17 { 18 if(fabs(x) < eps)return 0; 19 if(x < 0)return -1; 20 else return 1; 21 } 22 struct Point 23 { 24 double x,y; 25 Point() {} 26 Point(double _x,double _y) 27 { 28 x = _x; 29 y = _y; 30 } 31 Point operator -(const Point &b)const 32 { 33 return Point(x - b.x,y - b.y); 34 } 35 double operator ^(const Point &b)const 36 { 37 return x*b.y - y*b.x; 38 } 39 double operator *(const Point &b)const 40 { 41 return x*b.x + y*b.y; 42 } 43 }; 44 45 struct Line 46 { 47 Point s,e; 48 Line ( ) {} 49 Line (Point _s,Point _e) 50 { 51 s = _s; 52 e = _e; 53 } 54 }; 55 bool inter(Line l1,Line l2) 56 { 57 return 58 max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) && 59 max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) && 60 max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) && 61 max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) && 62 sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 && 63 sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0; 64 } 65 bool visit[100005]; 66 Line line[100005]; 67 int main(void) 68 { 69 int n; 70 while(scanf("%d",&n),n) 71 { 72 double x1,x2,y1,y2; 73 int sum = n; 74 memset(visit,false,sizeof(visit)); 75 for(int i = 1; i <= n; i++) 76 { 77 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 78 line[i] = Line(Point(x1,y1),Point(x2,y2)); 79 visit[i] = true; 80 } 81 for(int i = 1; i <= n; i++) 82 { 83 for(int j = i + 1; j <= n; j++) 84 { 85 if(inter(line[i],line[j])) 86 { 87 visit[i] = false; 88 sum--; 89 break; 90 } 91 } 92 } 93 printf("Top sticks:"); 94 for(int i = 1; i <= n; i++) 95 { 96 if(visit[i]) 97 { 98 if(sum != 1) 99 { 100 sum--; 101 printf(" %d,",i); 102 } 103 else 104 { 105 printf(" %d.\n",i); 106 sum = 0; 107 break; 108 } 109 } 110 } 111 } 112 return 0; 113 }