题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧。
思路:划分成两个凸包,一个红包,一个蓝包。两个凸包不相交不重合。
1.任取一个凸包中的点不在另一个凸包中。
2.任取一个凸包中的边与另一个凸包不相交。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<vector>
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long int
#define up(i,x,y) for(i=x;i<=y;i++)
#define w(a) while(a)
using namespace std;
const double inf=0x3f3f3f3f;
const int N = ;
const double eps = *1e-;
const double PI = acos(-1.0); double dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x),y(y) { }
}; typedef Point Vector; Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x-B.x, A.y-B.y);
} double Cross(const Vector& A, const Vector& B)
{
return A.x*B.y - A.y*B.x;
} double Dot(const Vector& A, const Vector& B)
{
return A.x*B.x + A.y*B.y;
} bool operator < (const Point& p1, const Point& p2)
{
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
} bool operator == (const Point& p1, const Point& p2)
{
return p1.x == p2.x && p1.y == p2.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),
c3 = Cross(b2-b1,a1-b1), c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
return dcmp(Cross(a1-p, a2-p)) == && dcmp(Dot(a1-p, a2-p)) < ;
} // 点集凸包
// 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
// 如果不介意点集被修改,可以改成传递引用
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 = ;
vector<Point> ch(n+);
for(int i = ; i < n; i++)
{
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-; i >= ; i--)
{
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
ch.resize(m);
return ch;
} int IsPointInPolygon(const Point& p, const vector<Point>& poly)
{
int wn = ;
int n = poly.size();
for(int i = ; i < n; i++)
{
const Point& p1 = poly[i];
const Point& p2 = poly[(i+)%n];
if(p1 == p || p2 == p || OnSegment(p, p1, p2)) return -; // 在边界上
int k = dcmp(Cross(p2-p1, p-p1));
int d1 = dcmp(p1.y - p.y);
int d2 = dcmp(p2.y - p.y);
if(k > && d1 <= && d2 > ) wn++;
if(k < && d2 <= && d1 > ) wn--;
}
if (wn != ) return ; // 内部
return ; // 外部
} bool ConvexPolygonDisjoint(const vector<Point> ch1, const vector<Point> ch2)
{
int c1 = ch1.size();
int c2 = ch2.size();
for(int i = ; i < c1; i++)
if(IsPointInPolygon(ch1[i], ch2) != ) return false; // 内部或边界上
for(int i = ; i < c2; i++)
if(IsPointInPolygon(ch2[i], ch1) != ) return false; // 内部或边界上
for(int i = ; i < c1; i++)
for(int j = ; j < c2; j++)
if(SegmentProperIntersection(ch1[i], ch1[(i+)%c1], ch2[j], ch2[(j+)%c2])) return false;
return true;
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m) == && n > && m > )
{
vector<Point> P1, P2;
double x, y;
for(int i = ; i < n; i++)
{
scanf("%lf%lf", &x, &y);
P1.push_back(Point(x, y));
}
for(int i = ; 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");
}
return ;
}