POJ 2653 Pick-up sticks(判断线段相交,有一点点小优化)
思路:就是判断线段与线段之间是否相交。
因为数据量很大,要一边遍历一边排除掉已经被压着的数据,这样的话才能卡着时间过。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include <cstdio>
using namespace std;
#define ll long long
struct Point
{
double x,y;
Point(double x= 0, double y = 0):x(x),y(y){}
};
typedef Point Vector;
const int INF = 0x3f3f;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator *(Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator +(Vector A, Vector B){
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator /(Vector A, double p){return Vector(A.x/p, A.y/p);}
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y;}
double Length(Vector A){ return sqrt(Dot(A, A));}
double Cross(Vector A, Vector B){return A.x*B.y-A.y*B.x;}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
const double eps = 1e-6;
int sgn(double x){
if(fabs(x) < eps)
return 0;
if(x < 0)
return -1;
return 1;
}
struct Line{
Point v, p;
Line(){}
Line(Point v, Point p):v(v), p(p){}
Point point(double t){
return v+(p-v)*t;//·µ»ØµãP = v + (p - v)*t
}
}line[105000];
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}
queue<int>q;
queue<int>temp;
int main()
{
int n;
while(scanf("%d",&n) !=EOF)
{
if(n == 0)
return 0;
for(int i = 1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&line[i].v.x, &line[i].v.y, &line[i].p.x, &line[i].p.y);
}
q.push(1);
for(int i = 2;i<=n;i++)
{
while(!q.empty())
{
int id = q.front();
q.pop();
if(!SegmentProperIntersection(line[id].v, line[id].p, line[i].v,line[i].p))
{
temp.push(id);
}
}
while(!temp.empty())
{
q.push(temp.front());
temp.pop();
}
q.push(i);
}
printf("Top sticks:");
int cnt = 0;
while(!q.empty())
{
if(cnt)
printf(",");
cnt++;
printf(" %d",q.front());
q.pop();
}
printf(".\n");
}
return 0;
}