UVALive 4992

#define eps 1e-12
#define inf 1e7
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct point{
    double x,y;
}p[50010],s[100010];
struct line{
    point a,b;
    double ang;
}ln[50010],deq[100010];

bool dd(double x,double y){
    return fabs(x-y)<eps;
}
double det(point a,point b,point c){
    return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dot(point a,point b,point c){
    return (c.x-a.x)*(b.x-a.x)+(c.y-a.y)*(b.y-a.y);
}
bool parallel(line l1,line l2){
    return dd((l1.a.x - l1.b.x)*(l2.a.y - l2.b.y) - (l1.a.y - l1.b.y)*(l2.a.x - l2.b.x),0.0);
}
point l2linst(line l1,line l2){
    point ans = l1.a;
    double t = ((l1.a.x - l2.a.x)*(l2.a.y - l2.b.y) - (l1.a.y - l2.a.y)*(l2.a.x - l2.b.x))/
    ((l1.a.x - l1.b.x)*(l2.a.y - l2.b.y) - (l1.a.y - l1.b.y)*(l2.a.x - l2.b.x));
    ans.x += (l1.b.x - l1.a.x)*t;
    ans.y += (l1.b.y - l1.a.y)*t;
    return ans;
}
bool equal_p(point a,point b){
    return dd(a.x,b.x)&&dd(a.y,b.y);
}
line make_line_hp(point a,point b){
    line l;
    l.a=a;l.b=b;l.ang=atan2(b.y-a.y,b.x-a.x);
    return l;
}
void inst_hp(line ln[],int n,point s[],int& len){
    len=0;
    int i,bot=0,top=1;
    deq[0]=ln[0];deq[1]=ln[1];
    for(i=2;i<n;i++){
        if(parallel(deq[top],deq[top-1])||parallel(deq[bot],deq[bot+1]))return ;
        while(bot<top&&det(ln[i].a,ln[i].b,l2linst(deq[top],deq[top-1]))>-eps)
            top--;
        while(bot<top&&det(ln[i].a,ln[i].b,l2linst(deq[bot],deq[bot+1]))>-eps)
            bot++;
        deq[++top]=ln[i];
    }
    while(bot<top&&det(deq[bot].a,deq[bot].b,l2linst(deq[top],deq[top-1]))>-eps)
        top--;
    while(bot<top&&det(deq[top].a,deq[top].b,l2linst(deq[bot],deq[bot+1]))>-eps)
        bot++;
    if(bot+1>=top)return ;
    for(i=bot;i<top;i++)
        s[len++]=l2linst(deq[i],deq[i+1]);
    if(bot<top+1)s[len++]=l2linst(deq[bot],deq[top]);
    len=unique(s,s+len,equal_p)-s;
}
bool bi(int d,int n){
    int i,top=0,m=4;
    for(i=0;i<n;i++)
        ln[top++]=make_line_hp(p[i],p[(i+d)%n]);
    s[0].x=-inf;s[0].y=-inf;
    s[1].x= inf;s[1].y=-inf;
    s[2].x= inf;s[2].y= inf;
    s[3].x=-inf;s[3].y= inf;
    s[4].x=-inf;s[4].y=-inf;
    inst_hp(ln,top,s,m);
    return m>0;
}
int main(){
    int i,n,l,r,mid;
    while(~scanf("%d",&n)){
        for(i=n-1;i>=0;i--)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        l=0;r=n+1;
        while(l<r){
            mid=l+r>>1;
            if(bi(mid,n))l=mid+1;
            else r=mid;
        }
        printf("%d\n",max(0,r-1));
    }
    return 0;

}

你可能感兴趣的:(UVALive 4992)