Grandpa's Estate POJ - 1228(凸包极角序改写)

本文介绍了一种解决特定几何问题的方法,通过构造凸包来判断一组点是否构成有效凸包,并分享了实现过程中的难点及注意事项。文章详细解释了如何修改极角排序算法以正确处理角度大于90度的情况,确保了算法的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看了题目意思之后感觉哇,简单的 一批不就是判断线段之间有没有至少一个点嘛,,,,,结果wa到自闭,,

这个题的点有首先要根据点构造凸包,因为题目给的数据不一定是一个凸包,其次,我们在构造凸包极角排序的时候当点与左下角的点的角度大于90度的时候会把距离近的点排在前面,所以要改写极角排序的算法,,然后遍历每个点判断就行了,,多个点共线输出no,,然后傻逼的自己wa了一个晚上在于凸包都写不对,,哭

bool cmp(Point a,Point b)
{
    int ans=sgn((a-p[0])^(b-p[0]));
    if(ans==1)
        return true;
    else if(ans==0)
    {
        if(sgn(atan2(a.y,a.x)-PI/2)>=0)
            return dist(a,p[0])>dist(b,p[0]);
        else
            return dist(a,p[0])<dist(b,p[0]);
    }
    else
        return false;
}
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

const double eps=1e-8;
const int maxn=40005;
const double PI=acos(-1.0);
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};


Point p[maxn];
int Stack[maxn];
int top=0;
int m;

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

bool cmp(Point a,Point b)
{
    int ans=sgn((a-p[0])^(b-p[0]));
    if(ans==1)
        return true;
    else if(ans==0)
    {
        if(sgn(atan2(a.y,a.x)-PI/2)>=0)
            return dist(a,p[0])>dist(b,p[0]);
        else
            return dist(a,p[0])<dist(b,p[0]);
    }
    else
        return false;
}

void graham()
{
    for(int i=0;i<m;i++)
    {
        if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
            swap(p[0],p[i]);
    }
    sort(p+1,p+m,cmp);
    if(m==1)
    {
        Stack[0]=0;
        top=0;
    }
    else if(m==2)
    {
        Stack[0]=0;
        Stack[1]=1;
        top=1;
    }
    else
    {
        Stack[0]=0;
        Stack[1]=1;
        top=1;
        for(int i=2;i<m;i++)
        {
            while(top>0&&((p[Stack[top]]-p[Stack[top-1]])^(p[i]-p[Stack[top-1]]))<0)
                top--;
            top++;
            Stack[top]=i;
        }
    }
}

bool vjudge()
{
    int cont=0;
    for(int i=1;i<=top;i++)
    {
        if(((p[Stack[i-1]]-p[Stack[i]])^(p[Stack[(i+1)%(top+1)]]-p[Stack[i]]))!=0)
        {
            cont++;
        }
    }
    if(cont==0)
        return false;
    cont=0;
    for(int i=1;i<=top;i++)
    {
        if(((p[Stack[i-1]]-p[Stack[i]])^(p[Stack[(i+1)%(top+1)]]-p[Stack[i]]))!=0)
        {
            if(cont==0)
                return false;
            cont=0;
        }
        else
            cont++;
        /*if(i==top)
        {
            if(cont==0)
                return false;
        }*/
    }
    return true;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        cin>>m;
        for(int i=0;i<m;i++)
            cin>>p[i].x>>p[i].y;
        if(m<6)
        {
            cout<<"NO"<<endl;
            continue;
        }
        graham();
        /*for(int i=0;i<=top;i++)
        {
            cout<<p[Stack[i]].x<<" "<<p[Stack[i]].y<<endl;
        }*/
        if(vjudge()==true)
        {
            cout<<"YES"<<endl;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值