codeforces 166 B. Polygons

来源:互联网 发布:淘宝答题抢红包 编辑:程序博客网 时间:2024/05/01 04:00

You've got another geometrical task. You are given two non-degenerate polygons A and B as vertex coordinates. Polygon A is strictly convex. Polygon B is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line.

Your task is to check whether polygon B is positioned strictly inside polygon A. It means that any point of polygon B should be strictly inside polygon A. "Strictly" means that the vertex of polygon B cannot lie on the side of the polygon A.

Input

The first line contains the only integer n (3 ≤ n ≤ 105) — the number of vertices of polygon A. Then n lines contain pairs of integersxi, yi (|xi|, |yi| ≤ 109) — coordinates of the i-th vertex of polygon A. The vertices are given in the clockwise order.

The next line contains a single integer m (3 ≤ m ≤ 2·104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj, yj (|xj|, |yj| ≤ 109) — the coordinates of the j-th vertex of polygon B. The vertices are given in the clockwise order.

The coordinates of the polygon's vertices are separated by a single space. It is guaranteed that polygons A and B are non-degenerate, that polygon A is strictly convex, that polygon B has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line.

Output

Print on the only line the answer to the problem — if polygon B is strictly inside polygon A, print "YES", otherwise print "NO" (without the quotes).

求凸包,然后判断凸包上是否有B的点,思路就是这么简单,就是你得首先知道怎么求凸包,然后这题你才有办法做

AC代码:

# include <stdio.h># include <vector># include <algorithm> using namespace std;typedef long long int ll;struct P{ll x, y, flage;};P s[200010];bool cmp(P a, P b);ll det(P p, P q);vector<P> convex_hull(P* ps, int n);P Minus(P a, P b);int main(){int n, m, i, j, k, sum;scanf("%d", &n);for(i=0; i<=n-1; i++){scanf("%I64d%I64d", &s[i].x, &s[i].y);s[i].flage=1;}scanf("%d", &m);sum=n+m-1;for(i=n; i<=sum; i++){scanf("%I64d%I64d", &s[i].x, &s[i].y);s[i].flage=2;}vector<P> ans=convex_hull(s, sum+1);for(i=0; i<ans.size(); i++){if(ans[i].flage==2){printf("NO");return 0;}}printf("YES");return 0;}P Minus(P a, P b){P c;c.x=a.x-b.x;c.y=a.y-b.y;return c;}vector<P> convex_hull(P* ps, int n){sort(ps, ps+n, cmp);int k=0;vector<P> qs(n*2);for(int i=0; i<n; i++){while(k>1&&det(Minus(qs[k-1],qs[k-2]), Minus(ps[i],qs[k-1]))<0)k--;qs[k++]=ps[i];}for(int i=n-2, t=k; i>=0; i--){while(k>t&&det(Minus(qs[k-1], qs[k-2]), Minus(ps[i], qs[k-1]))<0)k--;qs[k++]=ps[i];}qs.resize(k-1);    return qs;}ll det(P p, P q){return p.x*q.y-p.y*q.x;}bool cmp(P a, P b){if(a.x!=b.x){return a.x<b.x;}return a.y<b.y;}



0 0
原创粉丝点击