uva10256
uva10256
题意
平面上存在两种颜色的点,问是否存在一条直线,使得任取一个红点和一个蓝点都在直线的异侧,这条直线不经过任何点。
分析
对每种颜色的点求一发凸包,问题等价于判断两个多边形是否相交或相切。
- 判断是否有边相交
- 判断是否有点在另一个多边形内或边上
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的更多相关文章
- 【题解】The Great Divide [Uva10256]
[题解]The Great Divide [Uva10256] 传送门:\(\text{The Great Divide [Uva10256]}\) [题目描述] 输入多组数据,每组数据给定 \(n\ ...
- uva10256(计算几何)
省选前练模板系列: #include<cmath> #include<cstdio> #include<cstring> #include<iostream& ...
- UVA10256 The Great Divide
怎么又没人写题解,那我来贡献一发好了. 题目意思很简单,平面上有两种颜色的点,问你能否求出一条直线使两种颜色的点完全分开. 首先我们考虑两个点集相离的充要条件,这两个点集的凸包必须相离.(很好证明或者 ...
随机推荐
- [洛谷P3203][HNOI2010]弹飞绵羊
题目大意:有$n$个节点,第$i$个节点有一个弹力系数$k_i$,当到达第$i$个点时,会弹到第$i+k_i$个节点,若没有这个节点($i+k_i>n$)就会被弹飞.有两个操作: $x:$询问从 ...
- 导致SQL执行慢的原因
索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...
- [Leetcode] Populating next right pointer in each node 填充每个节点的右指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- input checkbox 多选 验证
# input checkbox 多选 验证``` <ul class="orderMsg_radio"> <!--单选--> <input valu ...
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
- Light OJ 1074:Extended Traffic(spfa判负环)
Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...
- intellij IDEA与springboot项目建立
概念问题: IntelliJ系中的Project相当于Eclipse系中的workspace.IntelliJ系中的Module相当于Eclipse系中的Project.IntelliJ中一个Proj ...
- 函数实现多个按钮控制一个div
<!DOCTYPE HTML><html><head> <meta http-equiv="txttent-Type" txttent=& ...
- php函数-shuffle
Shuffle()函数说明: -随机乱序现有数组并不保留键值: -shuffle()函数把数组中的元素按随机顺序重新排列,该函数为数组中的元素分配新的键名,已有键名将被删除. 语法说明: shuffl ...
- javascript中arguments的应用——不定项传参求和
<script type="text/javascript"> window.onload=function(){ function sum(){ var result ...