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
怎么又没人写题解,那我来贡献一发好了. 题目意思很简单,平面上有两种颜色的点,问你能否求出一条直线使两种颜色的点完全分开. 首先我们考虑两个点集相离的充要条件,这两个点集的凸包必须相离.(很好证明或者 ...
随机推荐
- perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志
perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志 http://blog.chinaunix.net/xmlrpc.php? ...
- Java的Properties使用及格式定义
java.util.Properties extends Hashtable<Object,Object> 方便读取 键值对 格式的文本资源工具 常用方法一览 初始化对象 new Prop ...
- GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL values from a group. It returns ...
- HDU4725:The Shortest Path in Nya Graph(最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- mycat 管理MySQL5.7主从搭建
1.首先安装MySQL ab: 192.168.6.163 master 192.168.6.167 slave master: vi /etc/opt/rh/rh-mysql57/my.cnf.d/ ...
- 程序员的那些问题---转载自veryCD
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过 ...
- Spring学习-- SpEL表达式
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...
- 51nod 1020 逆序排列——dp
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...
- USACO_1.1_Your_Ride_Is_Here_(字符串+水题)
描述 http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=ride 给出两个由大写字母组成,长度不大于$6$的字符串. 将字符串中的各字母的字典 ...
- CentOS 7 主机加固手册-下
CentOS 7 主机加固手册-上 CentOS 7 主机加固手册-中 CentOS 7 主机加固手册-下 0x1f 删除禁用非必要的服务 删除非必要的服务 # Remove yum remove ...