Kingdom of Kittens

This pit ah ... .... many seniors shining copied code (Behind his)
1, find a point of the convex hull when the convex hull of the space-time return. My people stupid.
2, I started seeking points and n are not strictly convex hull is compared, and their sucker! When n points will seek out collinear strange results.
Therefore, the convex hull is then required is determined for each point ah.
The idea is marvelous ah! Than I do not know where to go marvelous. .
First, look at the edge of the convex hull is strictly incorrect point of presence,
and then enumerate two sides,
the two sides may only be adjacent, or intermediate spacer and a side edge of this point no.
Then consider the check, a more obvious nature of the convex hull when the number of sides on GG, you can blind guess is 6
Honestly though I started this nature may have noticed but did not think much can be made simpler.
Then we look at the other side there are a few points, and can even come out here ,, a good triangle zz ah,
say x y + 1 to the middle of two points, we want to sentence the two points and the slope x, x + 1 and y, y + 1 can not be connected into a triangle
well. gone,
Please consciously ignored #define

#include <bits/stdc++.h>
#define yxn inline
#define INF 19970404
using namespace std;
typedef long long db;
yxn int sign(db k){
    if (k>0) return 1; else if (k<0) return -1; return 0;
}
yxn int cmp(db k1,db k2){return sign(k1-k2);}
yxn int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}
struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
};
yxn int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
yxn db cross(point k1,point k2){ return k1.x*k2.y-k1.y*k2.x;}
yxn db dot(point k1,point k2){ return k1.x*k2.x+k1.y*k2.y;}
yxn int onS(point k1,point k2,point q){return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0;}
yxn vector<point> ConvexHull(vector<point>&A,int flag=1){ // flag=0 不严格 flag=1 严格
    int n=A.size(); vector<point>ans(n*2);
    sort(A.begin(),A.end()); int now=-1;
    for (int i=0;i<A.size();i++){
        while (now>0&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } int pre=now;
    for (int i=n-2;i>=0;i--){
        while (now>pre&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } ans.resize(now); return ans;
}
int n,vis[100005];
point p[100005];
vector<point> v;
yxn bool check(int x,int y){
    int m = v.size();
    db p1 = cross(v[(x+1)%m]-v[x],v[(y+1)%m]-v[y]);
    if(p1==0)return false;
    if(p1<0)swap(x,y);
    if((x+1)%m!=y&&(x+2)%m!=y)return false;
    if((x+2)%m==y&&vis[(x+1)%m])return false;
    int cnt=0;
    for(int i=(y+1)%m;i!=x;i=(i+1)%m){
        cnt++;
    }
    if(cnt>=4)return false;
    else if(cnt==3){
        if(vis[(y+1)%m]||vis[(y+3)%m])return false;
        if(cross(v[(y+1)%m]-v[y],v[(y+3)%m]-v[(y+2)%m])>0&&cross(v[(y+3)%m]-v[(y+2)%m],v[(x+1)%m]-v[x])>0)return true;
        return false;
    }else if(cnt==2){
        if(vis[(y+1)%m]&&vis[(y+2)%m])return false;
        if(vis[(y+1)%m]&&cross(v[(y+2)%m]-v[(y+1)%m],v[(x+1)%m]-v[x])<=0)return false;
        if(vis[(y+2)%m]&&cross(v[(y+1)%m]-v[y],v[(y+3)%m]-v[(y+2)%m])<=0)return false;
        return true;
    }
    return true;
}
yxn bool check(){
    for(int i=0;i<n;i++){
        bool f=0;
        for(int j=0;j<v.size();j++){
            if(onS(v[j],v[(j+1)%v.size()],p[i]))f=1;
        }
        if(!f)return false;
    }
    return true;
}
int main(){
    while (scanf("%d",&n)&&n){
        v.clear();
        for(int i=0;i<n;i++)scanf("%lld%lld",&p[i].x,&p[i].y),v.push_back(p[i]);
        v=ConvexHull(v,1);
        if(v.size()>6)printf("NO\n");
        else if(v.size()<=2)printf("YES\n");
        else if(!check())printf("NO\n");
        else{
            int m = v.size();
            for(int i=0;i<m;i++)vis[i]=0;
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    if(cross(v[(i+1)%m]-v[i],p[j]-v[i])==0&&dot(p[j]-v[i],p[j]-v[(i+1)%m])<0) {
                        vis[i] = 1;
                        break;
                    }
                }
            }
            bool f=0;
            for(int i=0;i<v.size()&&!f;i++){
                for(int j=i+1;j<v.size()&&!f;j++){
                    f|=check(i,j);
                }
            }
            if(f)printf("YES\n");
            else printf("NO\n");
        }
    }
}

Guess you like

Origin www.cnblogs.com/MXang/p/11621910.html