您的位置:首页 > 其它

POJ 2653 Pick-up sticks(计算几何 求交点)

2012-09-06 11:27 465 查看
题意:依次给你n根棍子,问最上面的棍子是哪几根。

直接暴力,TLE,,用一个双向链表优化 500ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
const double EPS = 1e-6;
const double INF = 1e20;
struct cvector{
    double x,y;
    cvector(double a,double b){x=a,y=b;}
    cvector(){}
};
cvector operator+(cvector a,cvector b){
    return cvector(a.x+b.x,a.y+b.y);
}
cvector operator-(cvector a,cvector b){
    return cvector(a.x-b.x,a.y-b.y);
}
cvector operator*(double a,cvector b){
    return cvector(a*b.x,a*b.y);
}
double operator*(cvector a,cvector b)
{
    return a.x*b.x+a.y*b.y;
}
double operator^(cvector a,cvector b)
{
    return a.x*b.y-b.x*a.y;
}
double length(double t){return t>0?t:-t;}
double length(cvector t){return sqrt(t*t);}
struct cpoint{
    double x,y;
    cpoint(double a,double b){x=a,y=b;}
    cpoint(){}
};
cvector operator-(cpoint a,cpoint b){
    return cvector(b.x-a.x,b.y-a.y);
}
double dist(cpoint a,cpoint b)
{
    return length(b-a);
}
struct cline{
    cpoint a,b;
};
bool intersect(cline a,cline b){
    return ((a.a-b.a)^(b.b-b.a))*((a.b-b.a)^(b.b-b.a))<EPS&&((b.a-a.a)^(a.b-a.a))*((b.b-a.a)^(a.b-a.a))<EPS;
}
const int N = 100009;
int ans
;
cline line
;
int fron
,nex
;
int main()
{
    freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        {
            ans[i] = 1;nex[i] = i-1;fron[i-1] = i;
            scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
            for(int j=i-1;j>0;)
            {
                if(ans[j]&&intersect(line[j],line[i]))
                {
                    nex[fron[j]] = nex[j];
                    fron[nex[j]] = fron[j];
                    ans[j] = 0;
                }
                j = nex[j];
            }
        }
        fron
 = n+1;
        printf("Top sticks: ");
        bool ou = false;

        for(int i=fron[0];i<=n;)
        {
            if(ou) printf(", ");ou = true;
            printf("%d",i);
            i=fron[i];
        }
        printf(".\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: