Codeforces166B Polygons(凸包判断)
来源:互联网 发布:jsp中引入java代码 编辑:程序博客网 时间:2024/05/18 17:56
题目链接:http://codeforces.com/problemset/problem/166/B
有点麻烦的叉乘+二分判断法:http://blog.csdn.net/xuh723/article/details/22337441
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.
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.
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).
6-2 10 33 34 13 -22 -240 12 23 11 0
YES
51 24 23 -3-2 -2-2 140 11 24 12 -1
NO
5-1 22 34 13 -20 -351 01 13 15 -12 -1
NO
题意:给出两个多边形A,B,保证A是一个凸多边形,判断B是否完全包含在A内。
先将所有的点都存在一个数组p中,然后求出凸包
然后二分判断多边形B中的点是否在凸包上,如果在凸包上则说明B没有完全包含在A内,输出NO。如果凸包上没有多边形B上的点,则输出YES。
(代码中二分用了algorithm中的lower_bound)
(用的Andrew求凸包,没有任何细节和注意事项- -)
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8struct point{ double x,y; point(){} point(double _x,double _y) { x=_x;y=_y; } point operator - (const point &b) const { return point(x-b.x,y-b.y); } bool operator < (const point &b) const { return x<b.x||x==b.x&&y<b.y; }}res[120005],p[120005],pb[20005];int na,nb,n;int dcmp(double x){ return (x>eps)-(x<-eps);}double cross(point a,point b){ return a.x*b.y-b.x*a.y;}int andrew(){ sort(p,p+n); int m=0; for (int i=0;i<n;i++) { while (m>1&&cross(res[m-1]-res[m-2],p[i]-res[m-2])<0) --m; res[m++]=p[i]; } int k=m; for (int i=n-2;i>=0;--i) { while (m>k&&cross(res[m-1]-res[m-2],p[i]-res[m-2])<0) --m; res[m++]=p[i]; } if (m>1) --m; return m;}int main(){ while (scanf("%d",&na)!=EOF) { for (int i=0;i<na;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } scanf("%d",&nb); n=na+nb; for (int i=na;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); pb[i-na]=p[i]; } int m=andrew(); bool flag=1; sort(pb,pb+nb); for (int i=0;i<m;i++) { int tmp=lower_bound(pb,pb+nb,res[i])-pb;//返回第一个大于等于res[i]的pb的下标 if (dcmp(pb[tmp].x-res[i].x)==0&&dcmp(pb[tmp].y-res[i].y)==0) {flag=0;break;}//如果等于pb则说明B没有完全包含在A内,退出循环 } if (flag==1) printf("YES\n");else printf("NO\n"); } return 0;}
- Codeforces166B Polygons(凸包判断)
- Codeforces166B Polygons(叉乘+二分)
- HDU 1632 Polygons (凸包)
- Polygons
- The minimum distance between two convex polygons---两个凸包之间的最短距离---旋转卡壳法
- 判断凸包
- 判断线段相交,凸包
- poj 1389 Area of Simple Polygons(线段树做法)
- 【codechef】Chef and Polygons(灵活题,坑题)
- POJ 1389 Area of Simple Polygons(扫描线)
- [POJ1389]Area of Simple Polygons(扫描线+线段树)
- [Codeforces 166B] Polygons (点在凸多边形内)
- geometry-api-java 学习笔记(五)多边形 Polygons
- POJ 1228 凸包惟一性判断(多边形模板)
- 判断稳定凸包,求凸包代码(poj1228)
- uva10256 The Great Divide(凸包+判断)
- HDU 2108 Shape of HDU [凸包.判断凸包]
- hdu 2108 判断凹包还是凸包
- 算法 双堆维护中位数
- SDUTOJ 1197 约瑟夫问题——循环链表解法数学解法和循环队列
- 无法识别的属性“targetFramework”---解决之道
- ubuntu12.04卸载软件方法
- Nova提交BP的最新流程
- Codeforces166B Polygons(凸包判断)
- 解开socket编程的面纱
- Word Break II -- LeetCode
- ubuntu12.04格式化U盘或者SD卡方法
- EJB——总结
- Linux下Makefile文件的编程基础
- Windows SDK 01 简单的Dialog窗口
- 黑马程序员-IOS开发之--Object-c-- 内存管理二
- java学习之路