UVA 1475

当初看刘书时发现的题目,没有好好去理解= =

其实题目要求的是最大的n,使得不管炸掉n个塔之后依旧有核

该核——司令部

但是在二分的时候还是赶脚有点懵比,后来才发现要求的是可以将其炸出来

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

//精度控制
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

//点
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
};

//向量
typedef Point Vector;

//点-点==向量
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}

//向量+向量==向量 或 点+向量==点
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 Length(Vector v)
{
    return sqrt(v.x*v.x+v.y*v.y);
}

//返回v逆时针旋转90度的单位向量
Vector Normal(Vector v)
{
    double L=Length(v);
    return Vector(-v.y/L, v.x/L);
}

//叉积
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

//直线
struct Line
{
    Point p;
    Vector v;
    double ang;
    Line(){}
    Line(Point p,Vector v):p(p),v(v)
    {
        ang=atan2(v.y,v.x);
    }
    bool operator<(const Line &L)const
    {
        return ang<L.ang;
    }
};

//判断p是否在直线L左边(在直线上不算)
bool OnLeft(Line L,Point p)
{
    return Cross(L.v,p-L.p)>0;
}

//得到直线的交点
Point GetIntersection(Line a,Line b)
{
    Vector u=a.p-b.p;
    double t=Cross(b.v,u)/Cross(a.v,b.v);
    return a.p+a.v*t;
}

//求直线集合L构成的半平面交集,如果存在是一个凸包
int HalfplaneIntersection(Line *L,int n,Point *poly)
{
    //极角排序
    sort(L,L+n);

    int first=0,last=0;
    Point *p=new Point[n];
    Line *q=new Line[n];
    q[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];

        //如果新插入的直线与last直线平行,需要删除最右边的那条
        if(fabs(Cross(q[last].v,q[last-1].v))<eps)
        {
            last--;
            if(OnLeft(q[last],L[i].p)) q[last]=L[i];
        }

        //新插入直线与上一条直线构成last-1交点
        if(first<last) p[last-1]=GetIntersection(q[last-1],q[last]);
    }

    //同first直线排序尾部无效节点
    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;
}

const int N=50000+10;
int n;
Point p[N],poly[N];
//Vector v[N];
Line l[N];

int main()
{
    while(~scanf("%d",&n)){

        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        reverse(p,p+n);
        if(n==3){printf("1\n");continue;}
        int left=1,right=n-2;
        while(left!=right){
            int mid=left+(right-left)/2;
            for(int i=0;i<n;i++){
                l[i]=Line(p[i],p[(i+mid+1)%n]-p[i]);
            }
            int m=HalfplaneIntersection(l,n,poly);
            if(!m)right=mid;
            else left=mid+1;
        }
        printf("%d\n",left);
    }
    return 0;
}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值