返回

poj1228 Grandpa's Estate【凸包稳定性判断】

发布时间:2022-11-28 13:41:42 130

题目链接:​​http://poj.org/problem?id=1228​​​
题意:有t组样例,每组有n个点,让你判断这n个点构成的凸包是否稳定
解析:凸包是否稳定就是指,对于某一条边,加多一个点,是否能形成一个更大的凸包,也就是,凸包上的每条边必需至少有三个点,否则就是不稳定的凸包

#include 
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e5+100;
const double eps = 1e-10;
const double pi = acos(-1.0);
struct point
{
double x,y;
point() {}
point(double _x,double _y)
{
x = _x;
y = _y;
}
bool operator < (const point &b) const
{
if(y==b.y)
return x<b.x;
return y<b.y;
}
bool operator == (const point &b)const
{
return x==b.x && y==b.y;
}
}a[maxn],ans[maxn];
double x_mul(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(point p1,point p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point t1,point t2)
{
double tmp = x_mul(a[0],t1,t2);
if(tmp==0)
return dis(a[0],t1)<dis(a[0],t2);
else
return tmp>0;
}
int graham(int n)
{
sort(a,a+n);
sort(a+1,a+n,cmp);
ans[0] = a[0];
ans[1] = a[1];
ans[2] = a[2];
int top = 2;
for(int i=3;i<n;i++)
{
while(top>=2&&x_mul(ans[top-1],ans[top],a[i])<0)
top--;
ans[++top] = a[i];
}
return top;
}
bool slove(int n)
{
int top = graham(n);
if(top<3)
return false;
for(int i=1;i<top;i++)
{
if(x_mul(ans[i-1],ans[i+1],ans[i])!=0&&
x_mul(ans[i],ans[i+2],ans[i+1])!=0)
return false;
}
return true;
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf %lf",&a[i].x,&a[i].y);
if(slove(n))
puts("YES");
else
puts("NO");
}
return 0;
}

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
poj3348 Cows【凸包面积】 2022-11-28 13:23:27