CodeForces-166B Geometry Judges whether Polygons Are Inside Convex Hulls

Posted by harryman100 on Thu, 03 Oct 2019 16:04:16 +0200

Give you a polygon (not guaranteed to be a convex polygon), and then let you judge whether the polygon is strictly inside the convex hull, you just have to answer yes or no.
Violent judgment of whether each point of the polygon is too slow in the convex hull, in fact, it is only necessary to know whether the convex hull of the polygon is contained in the convex hull outside. Further, if the polygon is likely to run outside, then there must be some points that are not included by the convex hull, then the new convex hull formed by the points of the original convex hull must be different from the original convex hull. Put all these together and find a new convex hull.
Then, it is judged whether the convex hull is the same as the original one. Then, because it is strictly inclusive, there are two problems to be solved. One is that the point of the polygon falls on the edge of the convex hull. In this case, we only need to modify the judgment condition when the convex hull is in order to change the cross product less than or equal to the out-stack to the cross product less than the out-stack. The other is that the point of the polygon falls on the point of the convex hull. We save all the points. Finally, we sort the convex hull, and then divide it into two parts.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=LONG_LONG_MAX;
const int N=2e5+7; 
struct Point {
	double x,y;
	Point() {}
	Point(double _x,double _y):x(_x),y(_y) {}
	bool operator <(const Point &rhs) const {
		if(x<rhs.x) return 1;
		else if(x==rhs.x&&y<rhs.y) return 1;
		else return 0;
	}
	Point operator +(Point B) {
		return Point(x+B.x,y+B.y);
	} 
	Point operator -(Point B) {
		return Point(x-B.x,y-B.y);
	} 
	Point operator *(int k) {
		return Point(k*x,k*y);
	}
	double dot(Point B) {
		return x*B.x+y*B.y;
	}
	double det(Point B) {
		return x*B.y-y*B.x;
	}
}a[N],b[N],c[N],ch1[N],ch2[N];
int ConvexHull(Point a[],int n,Point ch[]) {
	sort(a+1,a+1+n);
	int tot=0;
	for(int i=1;i<=n;i++) {
		while(tot>1&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<0) tot--;
		ch[++tot]=a[i];
	}
	int ctrl=tot;
	for(int i=n-1;i>=1;i--) {
		while(tot>ctrl&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<0) tot--;
		ch[++tot]=a[i];
	}
	return tot-1;
}
int main() {
	int n,m;
	scanf("%d",&n);
 	for(int i=1;i<=n;i++) {
 		double x,y;
 		scanf("%lf%lf",&x,&y);
 		a[i].x=b[i].x=x;
 		a[i].y=b[i].y=y;
	}
	scanf("%d",&m);
	for(int i=n+1;i<=n+m;i++) {
		scanf("%lf%lf",&b[i].x,&b[i].y);
		c[i-n]=b[i];
	}
	int sz1=ConvexHull(a,n,ch1);
	int sz2=ConvexHull(b,n+m,ch2);
	bool ok=1;
	if(sz1!=sz2) ok=0;
	else {
		for(int i=1;i<=sz1;i++) {
			if(ch1[i].x!=ch2[i].x||ch1[i].y!=ch2[i].y)	{
				ok=0;
				break;
			}
		}
	}
	if(ok) {
		sort(ch1+1,ch1+1+sz1);  
		for(int i=1;i<=m;i++) {
			int id=lower_bound(ch1+1,ch1+1+sz1,c[i])-ch1;
			if(ch1[id].x==c[i].x&&ch1[id].y==c[i].y) {
				ok=0;
				break;
			}
		}
	}
	if(ok) puts("YES");
	else puts("NO"); 
	return 0;
}

Topics: less