题目:
题意:
给定一些凸包上的点,问能否确定唯一凸包
题解:
如何确定呢?三点确定一条直线,我们只要知道凸包上每一条边都有至少三个点就好了,而且顶点至少是三个啊
那我们先求个凸包,然后判断是否每条边都是三个点,注意每个点不一定只在一条线上(端点)
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1005;
struct po
{
double x,y;
po(double X=0,double Y=0){x=X; y=Y;}
}dian[N];
const double eps=1e-9;
int dcmp(double x)
{
if (x<=eps && x>=-eps) return 0;
return (x>0)?1:-1;
}
int vis[N],n,top,stack[N];
po operator -(po x,po y){return po(x.x-y.x,x.y-y.y);}
double cj(po x,po y){return x.x*y.y-x.y*y.x;}
bool operator <(const po &x,const po &y){return (x.x<y.x)||(x.x==y.x && x.y<y.y);}
void tb()
{
sort(dian+1,dian+n+1);
top=0;
for (int i=1;i<=n;i++)
{
while (top>1 && dcmp(cj(dian[stack[top]]-dian[stack[top-1]],dian[i]-dian[stack[top-1]]))<=0) top--;
stack[++top]=i;
}
int k=top;
for (int i=n-1;i>=1;i--)
{
while (top>k && dcmp(cj(dian[stack[top]]-dian[stack[top-1]],dian[i]-dian[stack[top-1]]))<=0) top--;
stack[++top]=i;
}
if (n>1) top--;
}
int main()
{
int T,j;scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d",&n);double x,y;
for (int i=1;i<=n;i++)
{
scanf("%lf%lf",&x,&y);
dian[i]=po(x,y);
}
tb();
for (int i=1;i<=n;i++)
for (j=1;j<=top;j++)
if (dcmp(cj(dian[i]-dian[stack[j]],dian[i]-dian[stack[j%top+1]]))==0) vis[j]++;
bool fff=1;
for (int i=1;i<=top;i++)
if (vis[i]<3){fff=0;break;}
if (fff && top>=3) printf("YES\n");else printf("NO\n");
}
}