题目链接:https://hihocoder.com/problemset/problem/1582
题意:给出n个点。用两种颜色(A,B)来给每个点染色。问能否存在一种染色方式,使不同颜色的点不能被划分到一条直线的两侧。
题解:求个凸包(点最大数的凸包,允许三个相邻点共线)。此题的YES其实就两种情况:
第一:如果不是所有点都在凸包上,那么把凸包上的点染成颜色A,内部的点染成颜色B;那么可能无法划分
第二:如果是所有点都在凸包上,且点数大于3个,那么将第一个和第三个点染成颜色A,其余点染成颜色A,此时就考虑前四个点,1,3同样颜色,2,4同样颜色,一定不能划分,考虑下是不是!!如果小于等于3个点,则输出NO,一定能划分。
第一份代码:(不是很懂四个共线的点求出的6个点的凸包)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
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;
int id;
Point(){}
Point(double x,double y):x(x),y(y){}
bool operator==(const Point& B)const
{
return dcmp(x-B.x)==0 && dcmp(y-B.y)==0;
}
bool operator<(const Point &B)const
{
return dcmp(x-B.x)<0 || (dcmp(x-B.x)==0 && dcmp(y-B.y)<0);
}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator+(Point A,Point 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;
}
int ConvexHull_1(Point *p,int n,Point *ch)//点数最少的凸包
{//该凸包的点集任意3点不会共线
sort(p,p+n);
n=unique(p,p+n)-p;
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--;//注意<=0
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;
}
int ConvexHull(Point *p,int n,Point *ch)//点数最多的凸包
{//该凸包的点集ch中可能相邻3点共线
sort(p,p+n);
n=unique(p,p+n)-p;
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--;//注意<0
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;
}
double dis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(Point a ,Point b,Point c)
{
if(dis(b,c)>dis(a,b)&&dis(b,c)>dis(a,c))
return true;
return false;
}
const int maxn=1000+5;
Point p[maxn],ch[maxn];
bool vis[maxn];
int main()
{
int T; scanf("%d",&T);
while(T--)
{
memset(vis,false,sizeof vis);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
p[i].id=i;
}
int m=ConvexHull(p,n,ch);
if(m<n)
{
for(int i=0;i<m;i++)
{
vis[ch[i].id]=true;
}
printf("YES\n");
for(int i=0;i<n;i++)
{
if(vis[i])
{
printf("A");
}
else
printf("B");
}
printf("\n");
}
else
{
if(m>3)
{
printf("YES\n");
vis[ch[0].id]=true;
vis[ch[2].id]=true;
for(int i=0;i<n;i++)
{
if(vis[i])
{
printf("A");
}
else
printf("B");
}
printf("\n");
}
else
printf("NO\n");
}
}
return 0;
}
第二份:将n个点是同一条直线的情况拿出来特判
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
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;
int id;
Point(){}
Point(double x,double y):x(x),y(y){}
bool operator==(const Point& B)const
{
return dcmp(x-B.x)==0 && dcmp(y-B.y)==0;
}
bool operator<(const Point &B)const
{
return dcmp(x-B.x)<0 || (dcmp(x-B.x)==0 && dcmp(y-B.y)<0);
}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator+(Point A,Point 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;
}
int ConvexHull_1(Point *p,int n,Point *ch)//点数最少的凸包
{//该凸包的点集任意3点不会共线
sort(p,p+n);
n=unique(p,p+n)-p;
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--;//注意<=0
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;
}
int ConvexHull(Point *p,int n,Point *ch)//点数最多的凸包
{//该凸包的点集ch中可能相邻3点共线
sort(p,p+n);
n=unique(p,p+n)-p;
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--;//注意<0
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;
}
double dis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(Point a ,Point b,Point c)
{
if(dis(b,c)>dis(a,b)&&dis(b,c)>dis(a,c))
return true;
return false;
}
const int maxn=1000+5;
Point p[maxn],ch[maxn],q[maxn];
bool vis[maxn];
int main()
{
int T; scanf("%d",&T);
while(T--)
{
memset(vis,false,sizeof vis);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
p[i].id=i;
q[i]=p[i];
}
sort(q,q+n);
if(n>=3&&Cross(q[0]-q[1],q[0]-q[2])==0)
{
printf("YES\n");
if(cmp(q[0],q[1],q[2]))
{
vis[q[0].id]=true;
}
else if(cmp(q[1],q[0],q[2]))
{
vis[q[1].id]=true;
}
else if(cmp(q[2],q[1],q[2]))
{
vis[q[2].id]=true;
}
for(int i=0;i<n;i++)
{
if(vis[i])
printf("A");
else
printf("B");
}
printf("\n");
continue;
}
int m=ConvexHull(p,n,ch);
if(m<n)
{
for(int i=0;i<m;i++)
{
vis[ch[i].id]=true;
}
printf("YES\n");
for(int i=0;i<n;i++)
{
if(vis[i])
{
printf("A");
}
else
printf("B");
}
printf("\n");
}
else
{
if(m>3)
{
printf("YES\n");
vis[ch[0].id]=true;
vis[ch[2].id]=true;
for(int i=0;i<n;i++)
{
if(vis[i])
{
printf("A");
}
else
printf("B");
}
printf("\n");
}
else
printf("NO\n");
}
}
return 0;
}