题目大意:按顺序给出n条线段的端点坐标,判断每条坐标是否在线段堆的最上面,按输入顺序输出在最上端的线段的编号
解题思路:顺序判断该条线段与之后输入的线段是否有交点
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
#include <set>
using namespace std;
const int maxn = 1e5+10;
const int eps = 1e-9;
int ans[maxn];
struct Point
{
double x,y;
Point(double x = 0, double y = 0) : x(x),y(y){}
}p1[maxn],p2[maxn];
typedef Point Vector;
Vector operator - (Point A, Point B)
{
return Vector(A.x-B.x, A.y-B.y);
}
double Cross(Vector A,Vector B)
{
return A.x * B.y - A.y * B.x;
}
int dcmp(double x)
{
if(fabs(x) < eps)
return 0;
else
return x > 0 ? 1 : -1;
}
bool SegmentProperIntersection(Point A,Point B,Point C,Point D)
{
double c1 = Cross(B-A,C-A);
double c2 = Cross(B-A,D-A);
double c3 = Cross(D-C,A-C);
double c4 = Cross(D-C,B-C);
return dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0;
}
int main()
{
int n;
while(scanf("%d",&n))
{
if(!n) break;
for(int i = 1; i <= n; i++)
scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
int tag;
int cnt = 0;
for(int i = 1; i <= n; i++)
{
tag = -1;
for(int j = i+1; j <= n; j++)
{
if(SegmentProperIntersection(p1[i],p2[i],p1[j],p2[j]))
{
tag = j;
break;
}
}
if(tag == -1)
ans[cnt++] = i;
}
printf("Top sticks: ");
for(int i = 0; i < cnt-1; i++)
printf("%d, ",ans[i]);
printf("%d.\n",ans[cnt-1]);
}
return 0;
}