HDU 1147 Pick-up sticks (叉乘判断线段相交)

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2624    Accepted Submission(s): 954


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.
HDU 1147 Pick-up sticks (叉乘判断线段相交)_第1张图片
 

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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define eps 1e-8
using namespace std;
struct point {
	double x, y;
};
struct line {
	point s, e;
};
typedef point vec;
line l[100100];
int top[100100];
double cross(vec a, vec b) {
	return a.x * b.y - a.y * b.x;
}
int main() {
	int n;
	while(~scanf("%d", &n) && n) {
		int i, j;
		for(i = 0; i < n; i++) {
			scanf("%lf %lf %lf %lf", &l[i].s.x, &l[i].s.y, &l[i].e.x, &l[i].e.y);
			top[i] = 1;
		}
		for(i = 0; i < n; i++) {
			for(j = i + 1; j < n; j++) {
				vec v1, v2, v3, v4, v5, v6;
				v1.x = l[i].e.x - l[i].s.x, v1.y = l[i].e.y - l[i].s.y;
				v2.x = l[j].s.x - l[i].s.x, v2.y = l[j].s.y - l[i].s.y;
				v3.x = l[j].e.x - l[i].s.x, v3.y = l[j].e.y - l[i].s.y;
				v4.x = l[j].e.x - l[j].s.x, v4.y = l[j].e.y - l[j].s.y;
				v5.x = l[i].s.x - l[j].s.x, v5.y = l[i].s.y - l[j].s.y;
				v6.x = l[i].e.x - l[j].s.x, v6.y = l[i].e.y - l[j].s.y;
				if(cross(v1, v2) * cross(v1, v3) < eps && cross(v4, v5) * cross(v4, v6) < eps) {
					top[i] = 0;
					break;
				}
			}
		}
		int cnt = 0;
		for(i = 0; i < n; i++) {
			if(top[i]) {
				cnt++;
			}
		}
		printf("Top sticks:");
		for(i = 0; i < n; i++) {
			if(top[i]) {
				printf(" %d", i + 1);
				cnt--;
				if(cnt) printf(",");
				else printf(".\n");
			}
		}
	}
	return 0;
}


你可能感兴趣的:(HDU 1147 Pick-up sticks (叉乘判断线段相交))