链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1197
题意:平面上n个红点,m个蓝点,问是否存在一条直线,使得任取一个红点和一个蓝点都在直线异侧。
思路:可以把《训练指南》上的两个思路换一下顺序,求出两个凸包之后,先判断一个凸包上的点是否在另一个凸包内侧,不需要判断所有红色的点是否在蓝色凸包内,只要判断红凸包上的顶点就够了。再来判断线段相交,这时只要判断是否是规范相交就好了。觉得数据有点水。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int maxn=510; const double eps=1e-8; struct Point { int x,y; Point(int x=0,int y=0):x(x),y(y) {} }; int n,m; Point red[maxn],blue[maxn]; typedef Point Vector; Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); } double Dot(Vector A,Vector B) { return A.x*B.x+A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A,A)); } double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } bool operator == (const Point& a,const Point& b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; } bool OnSegment(Point p,Point a1,Point a2) { return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0; } int cmp(const Point &a,const Point &b) { if(a.x<b.x) return 1; else if(a.x==b.x) { if(a.y<b.y) return 1; else return 0; } else return 0; } int ConvexHull(Point *p,int n,Point *ch) { 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(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--; ch[m++]=p[i]; } if(n>1) m--; return m; } int isPointInPolygon(Point p,Point* poly,int n) { int wn=0; for(int i=0;i<n;i++) { Point& p1=poly[i],p2=poly[(i+1)%n]; if(p==p1 || p==p2 || OnSegment(p,p1,p2)) return -1; int k=dcmp(Cross(p2-p1,p-p1)); int d1=dcmp(p1.y-p.y); int d2=dcmp(p2.y-p.y); if(k>0 && d1<=0 && d2>0) ++wn; if(k<0 && d2<=0 && d1>0) --wn; } if(wn) return 1; return 0; } bool SegmentProperIntersect(Point a1,Point a2,Point b1,Point b2) { double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1); double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0; } //Point read() //{ // Point p; // scanf("%d%d",&p.x,&p.y); // return p; //} void read(Point& p) { scanf("%d%d",&p.x,&p.y); } bool solve() { Point chr[maxn],chb[maxn]; int mr,mb; mr=ConvexHull(red,n,chr); mb=ConvexHull(blue,m,chb); for(int i=0;i<mr;i++) if(isPointInPolygon(chr[i],chb,mb)) return 0; for(int i=0;i<mb;i++) if(isPointInPolygon(chb[i],chr,mr)) return 0; for(int i=0;i<mr;i++) for(int j=0;j<mb;j++) { if(SegmentProperIntersect(chr[i],chr[(i+1)%mr],chb[j],chb[(j+1)%mb])) return 0; } return 1; } int main() { // freopen("ine.cpp","r",stdin); while(scanf("%d%d",&n,&m) && n && m) { for(int i=0;i<n;i++) read(red[i]);//red[i]=read();// for(int i=0;i<m;i++) read(blue[i]);//blue[i]=read();// if(solve()) printf("Yes\n"); else printf("No\n"); } return 0; }
究竟是我抛弃了历史,还是历史遗弃了我。