https://vjudge.net/problem/UVA-10256
翻了一个小时的uva,竟然没找到题库在哪儿,
蠢哭~~~~(>_<)~~~~
题意:
一堆蓝点一堆红点
能否找到一条直线,将蓝点与红点分开
构造出蓝点的凸包,红点的凸包
如果能的话
1、两凸包的所有边没有交点
2、蓝点不在红凸包内
3、红点不在蓝凸包内
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 501 struct Point { int x,y; Point (int x=0,int y=0) : x(x),y(y) { } bool operator < (Point q) { if(x!=q.x) return x<q.x; return y<q.y; } bool operator == (Point q) { return x==q.x && y==q.y; } }; typedef Point Vector; Point P1[N],P2[N],c1[N],c2[N]; Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); } void read(int &x) { x=0; int f=1; char c=getchar(); while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } int dcmp(int x) { if(!x) return 0; return x<0 ? -1 : 1; } int Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } int Dot(Vector A,Vector B) { return A.x*B.x+A.y*B.y; } bool cmp(Point A,Point B) { if(A.x==B.x) return A.y<B.y; return A.x<B.x; } int ConvexHull(Point *p,int n,Point *c) { sort(p,p+n,cmp); n=unique(p,p+n)-p; int m=0; for(int i=0;i<n;++i) { while(m>1 && Cross(c[m-1]-c[m-2],p[i]-c[m-2])<=0) m--; c[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;--i) { while(m>k && Cross(c[m-1]-c[m-2],p[i]-c[m-2])<=0) m--; c[m++]=p[i]; } return m; } bool OnSegment(Point p,Point a1,Point a2) { if(p==a1 || p==a2) return true; return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0; } bool SegmentIntersection(Point a1,Point a2,Point b1,Point b2) { if(OnSegment(a1,b1,b2) || OnSegment(a2,b1,b2) || OnSegment(b1,a1,a2) || OnSegment(b2,a1,a2)) return true; int c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0; } bool isPointInPolygon(Point p,Point *c,int n) { if(n==1) return false; if(n==3) return OnSegment(p,c[0],c[1]); for(int i=0;i<n-1;++i) if(dcmp(Cross(c[i+1]-c[i],p-c[i]))<0) return false; } bool check(int n1,int m1,int n2,int m2) { for(int i=0;i<m1-1;++i) for(int j=0;j<m2-1;++j) if(SegmentIntersection(c1[i],c1[i+1],c2[j],c2[j+1])) return false; for(int i=0;i<n1;++i) if(isPointInPolygon(P1[i],c2,m2)) return false; for(int i=0;i<n2;++i) if(isPointInPolygon(P2[i],c1,m1)) return false; return true; } int main() { int n1,n2; int m1,m2; while(1) { read(n1); read(n2); if(!n1) return 0; for(int i=0;i<n1;++i) read(P1[i].x),read(P1[i].y); for(int i=0;i<n2;++i) read(P2[i].x),read(P2[i].y); m1=ConvexHull(P1,n1,c1); m2=ConvexHull(P2,n2,c2); puts(check(n1,m1,n2,m2) ? "Yes" : "No"); } }