标签:form name lower ict win cti line cstring roc
题目链接:
http://poj.org/problem?id=2653
题目描述:
Description
Input
Output
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
题目大意:
按顺序往二维平面上扔线段,一根压在一根上面,求哪些线段在最顶端
思路:
每次新扔一个线段的时候,判断与当前在顶端的线段是否相交,若相交则把之前的那条删除
代码:
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 }
标签:form name lower ict win cti line cstring roc
原文地址:http://www.cnblogs.com/hyp1231/p/7004376.html