题目链接:点击打开链接
题目的意思很是简单,说给一个凸包上的点,判断一下是否为稳定凸包。是的话YES,不是的话NO。
稳定凸包概念:http://blog.csdn.net/u011394362/article/details/38303563
需要注意的是:1.共线的为NO。
2.顶点数小于6的为NO。
3.凸包上的每条边上的顶点小于3的为NO。
Code:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e3 + 5;
const double eps = 1e-8;
struct POINT{
double x, y;
}p[N];
double cross(POINT o, POINT a, POINT b){
return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
}
double Distance(POINT a, POINT b){
return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}
bool cmp(POINT a, POINT b){
if(a.y < b.y ||(a.y == b.y && a.x < b.x)) return true;
else return false;
}
//凸包上的一些点丢掉以后,仍然是一个凸包,但是不一定和原来是凸包一样。
int top, n;
POINT st[N];
void Graham_scan(){
sort(p, p + n, cmp);
top = 0;
st[top ++] = p[0]; st[top ++] = p[1];
for(int i = 2; i < n; i ++){//顺时针求一半
while(top >= 2 && cross(st[top - 1], st[top - 2], p[i]) > eps) {
top --;
}
st[top ++] = p[i];
}
int top1 = top;
for(int i = n - 2; i >= 0; i --){//逆时针求另一半
while(top != top1 && cross(st[top - 1], st[top - 2], p[i]) > eps) {
top --;
}
st[top ++] = p[i];
}
st[top] = p[1];
}
double Sum(){
double ans = 0;
for(int i = 1 ; i < top; i ++){
ans += cross(st[0], st[i], st[i - 1]);
}
return ans;
}
bool Judge(){
if(fabs(Sum()) < eps) return false;
// printf("%d\n", top);
for(int i = 1; i < top - 1; i ++){
if(fabs(cross(st[i], st[i - 1], st[i + 1])) > 0 && fabs(cross(st[i + 1], st[i], st[i + 2])) > 0) return false;
}
return true;
}
int main(){
// freopen("1.TXT", "r", stdin);
int T;
scanf("%d", &T);
while(T --){
scanf("%d", &n);
for(int i = 0; i < n; i ++){
scanf("%lf %lf", &p[i].x, &p[i].y);
}
if(n < 6) {//特判有无均可.
puts("NO");
continue;
}
Graham_scan();
if(Judge()) puts("YES");
else puts("NO");
}
return 0;
}
需要注意的是,这种求凸包的方法,求的是稳定凸包,也就是说,只要在凸包上的顶点,都在凸包之上。
以前的求凸包是方法求的是纯净凸包,也就是说,凸包上共线的顶点,只去”拐点处“。
写了以上午都没写好,有来一小时。。原来是越界错误了。。