Pick-up sticks
Time limit:3000 Ms |
|
Memory limit:65536 K |
Total submissions:7550 |
|
Accepted:2789 |
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
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 <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>//Accepted4116K641MSC++1841B2013-05-22 16:46:02using namespace std;const int maxn = 100010;struct Point { double x; double y;};struct V { bool mark; Point st; ///start; Point et; ///end V() { mark = true; }};V v[maxn];int n;double det(Point p1, Point p2, Point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}int Across(struct V v1, struct V v2) { if(max(v1.st.x, v1.et.x)>= min(v2.st.x, v2.et.x) && max(v2.st.x, v2.et.x)>= min(v1.st.x, v1.et.x) && max(v1.st.y, v1.et.y)>= min(v2.st.y, v2.et.y) && max(v2.st.y, v2.et.y)>= min(v1.st.y, v1.et.y) && det(v2.st, v1.et, v1.st)*det(v1.et, v2.et, v1.st)>=0 && det(v1.st, v2.et, v2.st)*det(v2.et, v1.et, v2.st)>=0 ) return 1; return 0;}void work() { for(int i = 1; i <= n-1; i++) { for(int j = i+1; j <= n; j++) { if(Across(v[i], v[j])) { v[i].mark = false; break; } } } vector<int> tmp; tmp.clear(); for(int i = 1; i <= n; i++) { if(v[i].mark) { tmp.push_back(i); } } printf("Top sticks: "); int maxsize = tmp.size(); for(int i = 0; i < maxsize-1; i++) { printf("%d, ", tmp[i]); } printf("%d.\n", tmp[maxsize-1]);}void init() { for(int i = 1; i <= n; i++) { v[i].mark = true; }}int main(){ while(scanf("%d", &n) != EOF) { if(n==0) break; init(); for(int i = 1; i <= n; i++) { scanf("%lf%lf%lf%lf", &v[i].st.x, &v[i].st.y, &v[i].et.x, &v[i].et.y); } work(); } return 0;}