D - Grandpa’s Estate POJ - 1228
当凸包上存在一条边上的点只有端点两个点的时候,这个凸包不是稳定的,因为它可以在这条边外再引入一个点,构成一个新的凸包。但一旦一条边上存在三个点,那么不可能再找到一个点使它扩展成一个新的凸包,否则构成的新多边形将是凹的。
也就是说只要凸包上的每一条边上至少有三个点就是稳定凸包了, 那就直接求凸包然后对凸包上的每条边的检查一次.
但是这里求凸包的方法应该要用Andrew才对, 我不知道为什么别人的Graham_scan也能过, 因为用Graham_scan的话下面这种情形就不能考虑到
p
[
1
]
,
p
[
8
]
,
p
[
9
]
p[1],p[8],p[9]
p[1],p[8],p[9]共线, 他会把
p
[
8
]
p[8]
p[8]弹出再把
p
[
9
]
p[9]
p[9]算入凸包里, 这样最后一条边上就只有
p
[
1
]
p[1]
p[1]和
p
[
9
]
p[9]
p[9]了, 无法理解为什么能过
代码:
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e3+10;
const double EPS=1e-8;
const double PI=acos(-1);
inline int sgn(double a){ return a < -EPS ? -1 : a > EPS; }
inline int cmp(double a, double b){ return sgn(a-b); }
long long mod=1e9+7;
int n,m,T;
struct Point;
typedef Point Vector;
struct Point{
double x,y;
Point(){}
Point(double a, double b):x(a),y(b){}
double len(){return sqrt(x*x+y*y);}
void read(){scanf("%lf%lf",&x,&y);}
Point operator+(const Vector v)const{return {x+v.x,y+v.y};}
Vector operator-(const Point p)const{return {x-p.x,y-p.y};}
double operator^(const Vector v)const{return x*v.y-y*v.x;}//叉乘
double operator*(const Vector v)const{return x*v.x+y*v.y;}//点乘
Vector operator*(const double d)const{return {x*d,y*d};}
Vector operator/(const double d)const{return {x/d,y/d};}
bool operator==(const Point p)const{return cmp(x,p.x)==0&&cmp(y,p.y)==0;}
bool operator<(const Point p)const{if(cmp(x,p.x)==0) return y<p.y;return x<p.x;}
};
Point p[1010],stk[1010];
int Andrew(){
sort(p+1,p+n+1);
int len=0;
for (int i=1;i<=n;i++){
while (len>1&&sgn((stk[len]-stk[len-1])^(p[i]-stk[len-1]))<0) len--;
stk[++len]=p[i];
}
int k=len;
for (int i=n-1;i>=1;i--){
while (len>k&&sgn((stk[len]-stk[len-1])^(p[i]-stk[len-1]))<0) len--;
stk[++len]=p[i];
}
len--;
return len;
}
void solve(){
if(n<6){
printf("NO\n");
return ;
}
int len=Andrew();
stk[0]=stk[len];
stk[len+1]=stk[1];
stk[len+2]=stk[2];
for(int i=1;i<=len;i++){
if(sgn((stk[i+2]-stk[i])^(stk[i+1]-stk[i]))==0||
sgn((stk[i+1]-stk[i-1])^(stk[i]-stk[i-1]))==0)
continue;
printf("NO\n");
return;
}
printf("YES\n");
}
void init(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
p[i].read();
}
int main(){
scanf("%d",&T);
while(T--){
init();
solve();
}
return 0;
}