The Great Divide
input: standard input
output: standard output
Time Limit: 8 seconds
Memory Limit: MB
Somewhere in Gaul, there are a little village very like the village where Asterix and Obelix live. Not very long ago they had only one chief Altruistix and Peace reigned in the village. But now those happy days is just dreams. The villagers is now divided. Some of the villagers has elected Majestix as their chief and the others have elected Cleverdix.
Majestix Cleverdix
The Chiefs has decided to divide, the village into, and parts by digging a straight ditch through the middle of the Vil Lage So, the houses of the supporters of Majestix lie on one part and those of the followers of Cleverdix lie on the other. So, they has invitedGetafix, the venerable Druid of Asterix ' s village, to figure out whether s Uch a dividing line exists or not.
Getafix
Since Getafix knows that's good in programming, he seeks your help.
Input
The input may contain multiple test cases.
The first line of all test case contains, integers M and C (1 £m, c £500) indicating the number of houses of the supporters ofMajestix and Cleverdix respectively.
Each of the next M lines contains the integers x and y ( -1000 £x, y £1000) giving the co-ordinates of the House of a supporter of Majestix. For convenience, each house was considered as a single point on the plane.
Each of the next C lines contains the integers x and y ( -1000 £x, y £10 XX) giving the co-ordinates of the House of a supporter ofCleverdix.
The input would terminate with a zeros for M and C.
Output
For each test, the input output a line containing either "Yes" or "No" depending on whether there exists a straight Line that divides the set of houses. The dividing line can not contain points of both sides.
Sample Input
4 3
100 600
200 400
600 500
300 700
400 100
600 200
500 300
4 3
100 600
400 100
600 200
500 300
200 400
600 500
300 700
0 0
Sample Output
Yes
No
Determines whether two point sets intersect, that is, there is no line segment can be divided into two point sets.
The convex hull is first obtained.
Determine if there is a point in the convex hull inside the other convex hull.
Determine if any two segments intersect within the convex hull.
Note that when sorting by coordinates, use dcmp to write.
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intN =533 ;Const DoubleEPS = 1e-Ten ;intN1, N2;intDCMP (Doublex) {if(Fabs (x) <eps)return 0;returnx<0?-1:1;}structPoint {Doublex, y; Point () {}; Point (DoubleADoubleb) {x=a, y=b;} BOOL operator< (ConstPoint &a)Const { returnDCMP (x-a.x) <0|| (dcmp (x-a.x) = =0&& dcmp (Y-A.Y) <0); }}p1[n],p2[n],ch1[n],ch2[n]; Pointoperator-(Point A, point B) {returnPoint (a.x-b.x,a.y-b.y);} Pointoperator+(Point A, point B) {returnPoint (a.x+b.x,a.y+b.y);}DoubleCross (Point A, point B) {returna.x*b.y-a.y*b.x;}DoubleDot A, point B) {returnA.X*B.X+A.Y*B.Y; }//TestBOOLispointonsegment (Point P, point A1, point A2) {returnDCMP (Cross (a1-p,a2-p)) = =0&& dcmp (Dot (a1-p,a2-p)) <0 ;}BOOLsegmentproperintersection (Point A1, Point A2, point B1, point B2) {DoubleC1 = Cross (A2-A1,B1-A1), C2 = Cross (a2-a1,b2-A1), C3= Cross (B2-B1,A1-B1), C4 = Cross (b2-b1,a2-B1); returnDCMP (C1) *dcmp (C2) <0&& dcmp (C3) *dcmp (C4) <0 ;}BOOLIspointinpolygon (point P, point* Poly,intN) {intWN =0 ; for(inti =0; I < n; ++i) {if(Ispointonsegment (P, Poly[i], poly[(i+1)%n]))return true; intK = dcmp (Cross (poly[(i+1)%n]-poly[i], P-poly[i])); intD1 = dcmp (Poly[i].y-p.y); intD2 = dcmp (poly[(i+1)%n].y-p.y); if(k >0&& d1<=0&& D2 >0) wn++ ; if(K <0&& D2 <=0&& D1 >0) wn--; } if(WN! =0)return true;//inside return false;//outside}intConvexhull (Point *p,intN,point *ch) {Sort (p,p+N); intm=0; for(intI=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]; } intk=m; for(inti=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--; returnm;}voidRun () { for(inti =0; I < N1; ++i) {scanf ("%LF%LF",&p1[i].x,&p1[i].y); } for(inti =0; I < N2; ++i) {scanf ("%LF%LF",&p2[i].x,&p2[i].y); } intM1 = Convexhull (p1,n1,ch1), M2 =Convexhull (P2,N2,CH2); for(inti =0; I < M1; ++i)if(Ispointinpolygon (CH1[I],CH2,M2)) {Puts ("No");return ;} for(inti =0; I < m2; ++i)if(Ispointinpolygon (CH2[I],CH1,M1)) {Puts ("No");return ;} for(inti =0; I < M1; ++i) { for(intj =0; J < m2; ++j) { if(Segmentproperintersection (ch1[i],ch1[(i+1)%m1],ch2[j],ch2[(j+1)%m2]) ) {puts ("No");return ; }}} puts ("Yes");}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); #endif //LOCAL while(SCANF ("%d%d", &n1,&n2) = =2&&N1) Run ();}
View Code
UVA 10256 the Great Divide (convex hull Division)