【稳定凸包】poj1228 Grandpa's Estate

来源:互联网 发布:大数据云计算百度云 编辑:程序博客网 时间:2024/04/29 03:07

Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 13719 Accepted: 3838

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

16 0 01 23 42 02 4 5 0

Sample Output

NO

Source


题意:求给出的点组成的凸包,是否为稳定凸包;凸包为稳定凸包的等价条件为凸包的每条边上至少有3个点;使用极角排序的Graham算法不如水平排序的Andrew算法更好的解决稳定凸包问题;(具体的可以参照刘汝佳的训练指南)

代码:

#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int n,tot;struct point{    double x,y;};point a[1005],p[1005];double dis(point A,point B){    return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));}double xmul(point A,point B,point C){    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);}int cmp(point A,point B){    return (A.x<B.x||(A.x==B.x&&A.y<B.y));}void Andrew(){    sort(a,a+n,cmp);    tot=0;    for(int i=0;i<n;i++)    {        while(tot>1&&xmul(p[tot-2],p[tot-1],a[i])<0) tot--; //加共线得点!        p[tot++]=a[i];    }    int k=tot;    for(int i=n-2;i>=0;i--)    {        while(tot>k&&xmul(p[tot-2],p[tot-1],a[i])<0) tot--;        p[tot++]=a[i];    }    if(n>1) tot--;}int judge(){    for(int i=1;i<tot;i++)    {        if(xmul(p[i],p[i-1],p[i+1])!=0&&xmul(p[i+1],p[i],p[i+2]))            return 0;    }    return 1;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%lf%lf",&a[i].x,&a[i].y);        Andrew();        judge();        if(n<6||tot<6) puts("NO");        else        {            if(judge()) puts("YES");            else puts("NO");        }    }    return 0;}