The Great Divid

【题目链接】The Great Divid

【题目类型】点在多边形内

&题解:

蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳的吧.

感觉这题最好的地方就是让我大致懂了点在多边形内的判断,写的好神奇,没有做一条直线,而是2个if判断就替代了这个,好腻害

&代码:

#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std; const double eps = 1e-10;
int dcmp(double x) {if(fabs(x)<eps) return 0; return x<0?-1:1;}
struct Point {
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
bool operator == (Point a,Point b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
bool operator < (Point a,Point b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);} bool SegmentProperIntersection(const Point& a1,const Point& a2,const Point& b1,const 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;
}
bool OnSegment(const Point& p,const Point& a1,const Point& a2) {
return dcmp(Cross(a1-p , a2-p))==0 && dcmp(Dot(a1-p , a2-p))<0;
} vector<Point> ConvexHull(vector<Point> p) {
sort(p.begin(),p.end());
p.erase(unique(p.begin(), p.end()), p.end());
int n=p.size();
int m=0;
vector<Point> ch(n+1);
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--;
ch.resize(m);
return ch;
} int IsInPolygon(const Point& p,const vector<Point>& poly) {
int n = poly.size() , wn=0;
for(int i=0; i<n; i++) {
const Point& p1 = poly[i] , p2 = poly[(i+1)%n];
if(p1==p || p2==p || OnSegment(p, p1, p2)) return -1;
int k=dcmp(Cross(p2-p1 , p-p1));
int d1=dcmp(p1.y - p.y) , d2=dcmp(p2.y - p.y);
if(k>0 && d1<=0 && d2>0) wn++;
if(k<0 && d2<=0 && d1>0) wn--;
}
return wn?1:0;
}
bool ConvexPolygonDisjoint(const vector<Point> ch1,const vector<Point> ch2) {
int c1=ch1.size(), c2=ch2.size();
for(int i=0; i<c1; i++) {
if(IsInPolygon(ch1[i],ch2)) return false;
}
for(int i=0; i<c2; i++) {
if(IsInPolygon(ch2[i],ch1)) return false;
}
for(int i=0; i<c1; i++) {
for(int j=0; j<c2; j++) {
if(SegmentProperIntersection(ch1[i],ch1[(i+1)%c1],ch2[j],ch2[(j+1)%c2])) return false;
}
}
return true;
}
int main() {
freopen("e:1.in","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)==2&&n>0&&m>0) {
vector<Point> P1,P2;
double x,y;
for(int i=0; i<n; i++) {
scanf("%lf%lf",&x,&y);
P1.push_back(Point(x,y));
}
for(int i=0; i<m; i++) {
scanf("%lf%lf",&x,&y);
P2.push_back(Point(x,y));
}
if(ConvexPolygonDisjoint(ConvexHull(P1), ConvexHull(P2)))
printf("Yes\n");
else
printf("No\n");
}
}

UVA 10256 The Great Divide(点在多边形内)的更多相关文章

  1. UVA 10256 The Great Divide (凸包,多边形的位置关系)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ...

  2. UVa 10256 - The Great Divide 判断凸包相交

    模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...

  3. UVA 10256 The Great Divide(凸包划分)

    The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ...

  4. uva 10256 The Great Divide

    题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧. 思路:划分成两个凸包,一个红包,一个蓝包.两个凸包不相交不重合. 1.任取一个凸包中的点不 ...

  5. UVa 10256 The Great Divide,推断两个凸包是否相离

    先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ...

  6. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  7. 【暑假】[数学]UVa 10375 Choose and divide

    UVa 10375 Choose and divide 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601 思路 ...

  8. PHP 判断点是否在多边形内

    如何判断一个点是否在一个多边形内,何时会用到这个场景. 我们就模拟一个真是场景.我们公司是快递公司,在本地区域有6个分点.每个分点有3-5个工人负责附近的快递派遣发送,所以根据每个点的服务区域我们就能 ...

  9. 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项

    首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ...

随机推荐

  1. [No0000EB]C# 数组(Array)

    数组是一个存储相同类型元素的固定大小的顺序集合.数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合. 声明数组变量并不是声明 number0.number1.....number99 一个 ...

  2. 关于ionic2 更新到ionic3 后组件不能用的解决方案

    错误代码就不贴出来了,直接上代码吧! 首先在xx.module.ts添加 1.import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 2.添加 ...

  3. [dpdk] dpdk多线程任务调度

    DPDK下的线程,叫做EAL线程. EAL线程默认是与CPU core一对一绑定的,这样的话,有一些实时性,计算量不高的任务独占CORE是一种浪费,大概找了如下几种解决方案. 1. dpdk seri ...

  4. 最大似然估计(Maximum likelihood estimation)(通过例子理解)

    似然与概率 https://blog.csdn.net/u014182497/article/details/82252456 在统计学中,似然函数(likelihood function,通常简写为 ...

  5. 转:JAVA守护线程

    原文地址:https://www.cnblogs.com/wxgblogs/p/5417503.html 详细内容看原文~  ,写的挺好的 在Java中有两类线程:User Thread(用户线程). ...

  6. Oracle shrink table

    shrink必须开启行迁移功能. alter table table_name enable row movement ; 在oracle中可以使用alter table table_name shr ...

  7. Gson使用技巧

    1. CharMatcher String serviceUrl = CharMatcher.is('/').trimTrailingFrom(ConfigHelper.metaServiceUrl( ...

  8. javascript高级技巧篇(作用域安全、防篡改、惰性载入、节流、自定义事件,拖放)

    安全的类型检测 在任何值上调用Object原生的toString()方法,都会返回一个[object NativeConstructorName]格式字符串.每个类在内部都有一个[[Class]]属性 ...

  9. 第四章:初识CSS3

    1.CSS规则由两部分构成,即选择器和声明器 声明必须放在{}中并且声明可以是一条或者多条 每条声明由一个属性和值构成,属性和值用冒号分开,每条语句用英文冒号分开 注意: css的最后一条声明,用以结 ...

  10. 5分钟简述Spring中的DI与AOP

    Spring的两个核心特性: 依赖注入(dependency injection,DI) 面向切面编程(aspect oriented programming,AOP) 依赖注入(dependency ...