UVA LA2797 二分加半平面交,很巧妙的二分

二分真是个神奇的东西,又是一个二分题目,而且这题分析的半平面交也比较巧妙,本人智商不够,二分和半平面交均想不到,

并且这题二分为什么会对,本渣渣也不太明白,留着慢慢的明白吧。

分析:

如果敌人只有一颗炸弹,你会把总部建在哪里呢?对于每个点,炸掉它以后不会暴露在外面的区域是一条有向直线的”左边“。这让我们想到了什么?

没错,半平面交!综合考虑所有点,所以建总部的范围就是所有这些半平面的交。

如果敌人有俩颗炸弹,总部应该建在哪里呢?分析后发现,敌人最聪明的做法是炸掉俩个连续的顶点,而不是分散火力(想一想,为什么)。这样,

每两个连续顶点对应一个新的半平面,可以建总部的范围仍然是所有半平面的交。

有了上面的分析,整个问题迎刃而解:二分答案,用上述方法判断大难是否可行(也就是半平面交是否为非空)。二分需要O(logn)时间,半平面交需要

O(nlogn)时间,总时间复杂度为O(n^2logn).

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const double eps=1e-16;
const int maxn=50000+10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x>0?1:-1;
    //return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1);
}

struct point
{
  double x;
  double y;
  point(){}
  point(double x,double  y):x(x),y(y){}
  void in()
  {
      cin>>x>>y;
  }
  void out()
  {
      cout<<x<<' '<<y<<endl;
  }
  point operator + (const point &t) const
  {
      return point(x+t.x,y+t.y);
  }
  point operator - (const point &t) const
  {
      return point(x-t.x,y-t.y);
  }
  point operator * (const double &t) const
  {
      return point(x*t,y*t);
  }
};
double cross(point a,point b)
{
    return a.x*b.y-a.y*b.x;
}
double dot(point a,point b)
{
    return a.x*b.x+a.y*b.y;
}
double length(point a)
{
    return sqrt(dot(a,a));
}
point nomal(point t)
{
    double l=length(t);
    return  point(-t.y/l,t.x/l);
}
struct line
{
    point p;
    point v;
    double ang;
    line() {}
    line(point p,point v):p(p),v(v){
        ang=atan2(v.y,v.x);
    }
    bool operator < (const line &l) const
    {
        return ang<l.ang;
    }
};

bool onleft(line l,point p)
{
    return cross(l.v,p-l.p)>0;
}
point getintersection(line a,line b)
{
    point u=a.p-b.p;
    double t=cross(b.v,u)/cross(a.v,b.v);
    return a.p+a.v*t;
}
point p[maxn];
line q[maxn];
point poly[maxn],pt[maxn];
line l[maxn];
int halfplaneintersection(line *l,int n,point *poly)
{
    sort(l,l+n);

    int first,last;
  //  point *p=new point[n];
 //   line *q=new  line[n];
    q[first=last=0]=l[0];
    for(int i=1;i<n;i++)
    {
        while(first<last && !onleft(l[i],p[last-1])) last--;
        while(first<last && !onleft(l[i],p[first])) first++;
        q[++last]=l[i];
        if(fabs(cross(q[last].v,q[last-1].v))<eps)
        {
            last--;
            if(onleft(q[last],l[i].p)) q[last]=l[i];
        }
        if(first<last) p[last-1]=getintersection(q[last-1],q[last]);
    }
    while(first<last && !onleft(q[first],p[last-1])) last--;
    if(last-first<=1) return 0;
    p[last]=getintersection(q[last],q[first]);
    int m=0;
    for(int i=first;i<=last;i++) poly[m++]=p[i];
    return m;
}


int slove(int n)
{
    if(n==3)
        return 1;
    int ll=1,r=n-3;
    while(ll<=r)
    {
        int c=0;
        int m=ll+r>>1;
        for(int i=0;i<n;i++)
            l[c++]=line(pt[(i+m+1)%n],pt[i]-pt[(i+m+1)%n]);
        if(halfplaneintersection(l,c,poly))
            ll=m+1;
        else
            r=m-1;
    }
    return ll;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
            pt[i].in();
        cout<<slove(n)<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值