uva10256

题意

平面上存在两种颜色的点,问是否存在一条直线,使得任取一个红点和一个蓝点都在直线的异侧,这条直线不经过任何点。

分析

对每种颜色的点求一发凸包,问题等价于判断两个多边形是否相交或相切。

  1. 判断是否有边相交
  2. 判断是否有点在另一个多边形内或边上

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double INF = 1e18;
const int MAXN = 6e2 + 10;
const double EPS = 1e-9;
int Sgn(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) {}
bool operator < (const Point& p1) const {
if(x == p1.x) return y < p1.y;
return x < p1.x;
}
void read_point() {
scanf("%lf%lf", &x, &y);
}
};
typedef Point Vector;
double Dot(Point p1, Point p2) {
return p1.x * p2.x + p1.y * p2.y;
}
double Cross(Point p1, Point p2) {
return p1.x * p2.y - p1.y * p2.x;
}
Point operator - (Point p1, Point p2) {
return Point(p1.x - p2.x, p1.y - p2.y);
}
// 判断点是否在线段上(不包括端点)
bool OnSegment(Point P, Point a1, Point a2) {
Vector v1 = a1 - P, v2 = a2 - P;
return Sgn(Cross(v1, v2)) == 0 && Sgn(Dot(v1, v2)) < 0;
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, 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 Sgn(c1) * Sgn(c2) < 0 && Sgn(c3) * Sgn(c4) < 0;
}
int ConvexHull(Point* p, int n, Point* ch) {
sort(p, p + n);
int m = 0;
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--;
return m;
}
//判定点P是否在多边形内部
int isPointInPolygon(Point P, Point* Poly, int n) {
int wn = 0;
for(int i = 0; i < n; ++i) {
if(OnSegment(P, Poly[i], Poly[(i + 1) % n])) return -1; //在边界上
int k = Sgn(Cross(Poly[(i + 1) % n] - Poly[i], P - Poly[i]));
int d1 = Sgn(Poly[i].y - P.y);
int d2 = Sgn(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 1; //内部
return 0; //外部
} Point p1[MAXN], p2[MAXN], ch1[MAXN], ch2[MAXN];
int main() {
int n, m;
while(~scanf("%d%d", &n, &m) && (n + m)) {
for(int i = 0; i < n; i++) {
p1[i].read_point();
}
for(int i = 0; i < m; i++) {
p2[i].read_point();
}
int nn = ConvexHull(p1, n, ch1);
int mm = ConvexHull(p2, m, ch2);
int flg = 1;
for(int i = 0; i < nn; i++) {
if(isPointInPolygon(ch1[i], ch2, mm)) flg = 0;
for(int j = 0; j < mm; j++) {
if(SegmentProperIntersection(ch1[i], ch1[(i + 1) % nn], ch2[j], ch2[(j + 1) % mm])) {
flg = 0;
}
}
}
for(int i = 0; i < mm; i++) {
if(isPointInPolygon(ch2[i], ch1, nn)) flg = 0;
}
puts(flg ? "Yes" : "No");
}
return 0;
}

uva10256的更多相关文章

  1. 【题解】The Great Divide [Uva10256]

    [题解]The Great Divide [Uva10256] 传送门:\(\text{The Great Divide [Uva10256]}\) [题目描述] 输入多组数据,每组数据给定 \(n\ ...

  2. uva10256(计算几何)

    省选前练模板系列: #include<cmath> #include<cstdio> #include<cstring> #include<iostream& ...

  3. UVA10256 The Great Divide

    怎么又没人写题解,那我来贡献一发好了. 题目意思很简单,平面上有两种颜色的点,问你能否求出一条直线使两种颜色的点完全分开. 首先我们考虑两个点集相离的充要条件,这两个点集的凸包必须相离.(很好证明或者 ...

随机推荐

  1. linux bash善用判断式

    1.利用 test 指令的测试功能 $ test -e hello.sh && echo "ok" || echo "no" ok 2.首先,判 ...

  2. boost 文件操作

    void testFileSystem() { boost::filesystem::path path("/test/test1"); //初始化 boost::filesyst ...

  3. php文件上传错误代码

    注意: 1.上传文件的时候,在html里面的form表单一定要标注:enctype='multipart/form-data' 2.有种说法,要求一定要在form表单里面,在file前面加上隐藏域如: ...

  4. 初识 spl_autoload_register

    spl_autoload_register 一.首先我们看来自官网的定义 版本要求:php版本为5.1.2+ 说明:注册给定的函数作为__autoload的实现.即自动加载 函数参数说明: bool ...

  5. Linux下文件解压命令

    1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -zxvf 压缩 ...

  6. HDU2066一个人的旅行---(多起点多终点最短路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memo ...

  7. 51nod 1254 最大子段和 V2 ——单调栈

    N个整数组成的序列a[1],a[2],a[3],…,a[n],你可以对数组中的一对元素进行交换,并且交换后求a[1]至a[n]的最大子段和,所能得到的结果是所有交换中最大的.当所给的整数均为负数时和为 ...

  8. HDU1267 下沙的沙子有几粒? 基础DP

    题目链接 题意:给定m个H和n个D(1<=n,m<=20),问这些字母构成的序列中,对于任意位置,从左开始数H的累积个数总是不比D的累计数少的排列有多少种. 题解:二维DP,画一个正方形, ...

  9. DotNETCore 学习笔记 日志

    Logging --------------------------------------------------------------------------------------- Impl ...

  10. EL表达式中获取list长度(JSTL函数用法)

    在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...