hdu 1147 pick up sticks 几何问题线段相交

来源:互联网 发布:安装linux u盘坏了 编辑:程序博客网 时间:2024/06/05 11:51

Pick-up sticks

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


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 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
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.
 

Source
University of Waterloo Local Contest 2005.09.17


 
//hdu 1147//#include"stdafx.h"#include<iostream>#include<algorithm>using namespace std;#define size 100010struct point {double x, y;};struct stick{point startpos;point endpos;};stick a[size];bool inter[size];double min(double x, double y) {return x > y ? y : x;}double max(double x, double y) {return x > y ? x : y;}//计算叉积函数double mult(point a, point b, point c) {return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);}//判断两线段是否相交  相交返回truebool isIntersect(point a,point b,point c,point d) {if (max(a.x, b.x)<min(c.x, d.x))return false;if (max(a.y, b.y)<min(c.y, d.y))return false;if (max(c.x, d.x)<min(a.x, b.x))return false;if (max(c.y, d.y)<min(a.y, b.y))return false;if (mult(c, b, a)*mult(b, d, a)<0)return false;if (mult(a, d, c)*mult(d, b, c)<0)return false;return true;}int main() {int n;while (cin >> n&&n) {memset(a, 0, sizeof(a));memset(inter, false, sizeof(inter));for (int i = 1; i <= n; i++) {cin >> a[i].startpos.x >> a[i].startpos.y >> a[i].endpos.x >> a[i].endpos.y;}for (int i = 1; i <= n - 1; i++) {for (int j = i + 1; j <= n; j++) {if (inter[i])break;//直接break掉 写成continue会超时........//如果相交 则被覆盖 置为trueif (isIntersect(a[i].startpos, a[i].endpos, a[j].startpos, a[j].endpos)) {inter[i] = true;}}}//输出cout << "Top sticks: ";for (int i = 1; i <= n - 1; i++) {if (!inter[i]) {cout << i << ", ";}}cout << n << "." << endl;}return 0;}

阅读全文
1 0
原创粉丝点击