Grandpa’s Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14447 Accepted: 4040
Description
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa’s belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa’s birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
Output
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
Sample Input
1
6
0 0
1 2
3 4
2 0
2 4
5 0
Sample Output
NO
Source
Tehran 2002 Preliminary
题意:给你一组点判断这几个点的确定的凸包是不是唯一的
做法:当一个凸包不能通过在外部加顶点,向外部扩大的时候,这时确定的凸包就是唯一的,这个叫做稳定凸包
一个凸包是稳定凸包的条件是,它的每条边上至少有3个顶点(共线)我的判断方法是,定义一个变量是cnt=0,记录和两边的点不共线的连续点个数,如果cnt等于2,那就说明肯定有一条边上只有两个点,这个凸包就不是稳定凸包了
原来的Graham是没有共线的点的,我们要修改一下求凸包的方法,把共线的也加入凸包,这时我们只要把出栈条件改为<即可,去掉等号就可以了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#define maxn 10010
#define maxe 100010
typedef long long ll;
using namespace std;
const double eps=1e-5;
const int inf=0x3f3f3f3f3f;
typedef double T1;
struct Point
{
T1 x,y;
Point(){};
Point(T1 a,T1 b)
{
x=a,y=b;
}
void input()
{
scanf("%lf%lf",&x,&y);
}
Point operator +(Point a)
{
Point b(x+a.x,y+a.y);
return b;
}
Point operator -(Point a)
{
Point b(x-a.x,y-a.y);
return b;
}
T1 operator *(Point a)
{
return x*a.x+y*a.y;
}
T1 operator ^(Point a)
{
return x*a.y-y*a.x;
}
bool operator <(Point a)
{
return x<a.x;
}
}p[maxn];
//两点距离
double dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
return 1;
}
//三点两线叉积
double xmult(Point p0,Point p1,Point p2)
{
return (p1-p0)^(p2-p0);
}
Point st[maxn];
int top=0;
bool cmp(Point a,Point b)
{
double tmp=xmult(p[0],a,b);
if(sgn(tmp)==0)return dist(a,p[0])<dist(b,p[0]);
//若共线返回距离小的
return sgn(tmp)<0;
}
void Graham(int n)
{
Point p0=p[0];
int k=0;
top=0;
for(int i=0;i<n;i++)
{
if(p[i].y<p0.y||(p[i].y==p0.y&&p[i].x<p0.x))
{
p0=p[i];
k=i;
}
}
swap(p[0],p[k]);
sort(p+1,p+n,cmp);
st[top++]=p[0];
st[top++]=p[1];
for(int i=2;i<n;i++)
{
while(top>1&&sgn(xmult(p[i],st[top-1],st[top-2]))<0)
//这里<=改为了<
{
top--;
}
st[top++]=p[i];
}
}
bool cal()
{
int cnt=0;
for(int i=0;i<top;i++)
{
if(xmult(st[i],st[(i-1+top)%top],st[(i+1)%top])==0)
{
cnt=0;
}
else cnt++;
if(cnt==2)return false;
}
return true;
}
int main()
{
int n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
p[i].input();
}
//特判,最小的稳定凸包是三角形每个边上还有另外一个点,共有6个点,n小于6一定不是稳定凸包
if(n<6){puts("NO");continue;}
Graham(n);
if(cal())puts("YES");
else puts("NO");
}
return 0;
}