码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2653 Pick-up sticks(线段相交)

时间:2017-06-14 02:14:17      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:form   name   lower   ict   win   cti   line   cstring   roc   

题目链接:

  http://poj.org/problem?id=2653

题目描述:

Pick-up sticks
 

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
0

Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

 

题目大意:

  按顺序往二维平面上扔线段,一根压在一根上面,求哪些线段在最顶端

思路:

  每次新扔一个线段的时候,判断与当前在顶端的线段是否相交,若相交则把之前的那条删除

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9 
10 const int N = 100010;
11 
12 const double EPS = 1e-10;    //精度系数
13 
14 struct Point {
15     double x, y;
16     Point(double x = 0, double y = 0) :x(x), y(y) {}
17 };    //点的定义
18 
19 typedef Point Vector;    //向量的定义
20 
21 int dcmp(double x) {
22     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
23 }    //与0的关系
24 
25 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
26 
27 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
28 
29 bool SegmentProperIntersection(Point& a1, Point& a2, Point& b1, Point& b2) {
30     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
31         c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
32     return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
33 }    //判断两线段相交(恰有一个公共点且不在端点)
34 
35 int n;
36 Point A[N], B[N];
37 
38 int main() {
39     while (cin >> n&&n) {
40         vector<int> ve;
41         for (int i = 1; i <= n; ++i) {
42             scanf("%lf%lf%lf%lf", &A[i].x, &A[i].y, &B[i].x, &B[i].y);
43             queue<int> q;
44             for (int j = 0; j < (int)ve.size(); ++j)    //枚举当前顶端线段
45                 if (SegmentProperIntersection(A[i], B[i], A[ve[j]], B[ve[j]]))
46                     q.push(ve[j]);
47             while (!q.empty()) {
48                 int t = q.front(); q.pop();
49                 vector<int>::iterator it = lower_bound(ve.begin(), ve.end(), t);
50                 ve.erase(it);
51             }
52             ve.push_back(i);
53         }
54         printf("Top sticks: ");
55         for (int i = 0; i < (int)ve.size(); ++i) {
56             if (i)printf(", ");
57             printf("%d", ve[i]);
58         }
59         printf(".\n");
60     }
61 }

 

POJ2653 Pick-up sticks(线段相交)

标签:form   name   lower   ict   win   cti   line   cstring   roc   

原文地址:http://www.cnblogs.com/hyp1231/p/7004376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!