[poj1279]Art Gallery

题意:求多边形的核的面积。

敲一下半平面交模板........  然后我wa了一早上就因为写了%lf  不知道poj什么破机制还不能用lf的,真的想跳楼

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define MN 50000
#define eps 1e-10
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

struct P{
    double x,y;
    P(double _x=0,double _y=0):x(_x),y(_y){}
    P operator + (P b) {return P(x+b.x,y+b.y);}
    P operator - (P b) {return P(x-b.x,y-b.y);} 
    P operator * (double b) {return P(x*b,y*b);}
    friend double cross(P a,P b) {return a.x*b.y-b.x*a.y;}
}p[MN+5],pt[MN+5];

struct L{
    P p,v;double slop;
    L(){}
    L(P x,P y):p(x),v(y){slop=atan2(y.y,y.x);}
    P operator * (L b){P a=p-b.p;double t=cross(b.v,a)/cross(v,b.v);return p+v*t;}
    bool left(P b){ return cross(v,b-p)>eps;}
    bool operator < (const L &y) const {return slop<y.slop;} 
}s[MN+5],q[MN+5];

int n,top,tail;

void solve()
{
    q[top=tail=1]=s[1];
    for(int i=2;i<=n;i++)
    {
        while(top>tail&&!s[i].left(p[top])) --top;
        while(top>tail&&!s[i].left(p[tail+1])) ++tail;
        if(fabs(s[i].slop-q[top].slop)<eps) 
            q[top]=s[i].left(q[top].p)?q[top]:s[i];
        else q[++top]=s[i];
        p[top]=q[top]*q[top-1];
    }
    while(tail<top&&(q[tail].left(p[top])==0)) --top;
}

int main()
{
    for(int T=read();T;T--)
    {
        n=read();    
        for(int i=1;i<=n;i++) scanf("%lf%lf",&pt[i].x,&pt[i].y);
        for(int i=2;i<=n;i++) s[i]=L(pt[i-1],pt[i]-pt[i-1]);
        s[1]=L(pt[n],pt[1]-pt[n]);
        sort(s+1,s+n+1); solve();
        if(top-tail+1<3) 
        {    
            for(int i=1;i<n;i++) s[i]=L(pt[i+1],pt[i]-pt[i+1]);
            s[n]=L(pt[1],pt[n]-pt[1]);
            sort(s+1,s+n+1);solve();
        }
        if(top-tail+1<3) puts("0.00");
        else 
        { 
            p[tail]=q[tail]*q[top];double area=0;
            for(int i=tail;i<top;i++) area+=cross(p[i],p[i+1]);
            area+=cross(p[top],p[tail]);
            printf("%.2f\n",fabs(area)/2.00+eps);
        }
    }
    return 0;
}

 

posted @ 2017-03-29 13:07  FallDream  阅读(195)  评论(0编辑  收藏  举报