# ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E Territorial Dispute （凸包）

923人阅读 评论(0)

### 描述

In 2333, the C++ Empire and the Java Republic become the most powerful country in the world. They compete with each other in the colonizing the Mars.

There are n colonies on the Mars, numbered from 1 to n. The i-th colony's location is given by a pair of integers (xi, yi). Notice that latest technology in 2333 finds out that the surface of Mars is a two-dimensional plane, and each colony can be regarded as a point on this plane. Each colony will be allocated to one of the two countries during the Mars Development Summit which will be held in the next month.

After all colonies are allocated, two countries must decide a border line. The Mars Development Convention of 2048 had declared that: A valid border line of two countries should be a straight line, which makes coloniesof different countries be situated on different sides of the line.

The evil Python programmer, David, notices that there may exist a plan of allocating colonies, which makes the valid border line do not exist. According to human history, this will cause a territorial dispute, and eventually lead to war.

David wants to change the colony allocation plan secretly during the Mars Development Summit. Now he needs you to give him a specific plan of allocation which will cause a territorial dispute. He promises that he will give you 1000000007 bitcoins for the plan.

### 输入

The first line of the input is an integer T, the number of the test cases (T ≤ 50).

For each test case, the first line contains one integer n (1 ≤ n ≤ 100), the number of colonies.

Then n lines follow. Each line contains two integers xi, yi (0 ≤ xi, yi ≤ 1000), meaning the location of the i-th colony. There are no two colonies share the same location.

There are no more than 10 test cases with n > 10.

### 输出

For each test case, if there exists a plan of allocation meet David's demand, print "YES" (without quotation) in the first line, and in the next line, print a string consisting of English letters "A" and "B". The i-th character is "A" indicates that the i-th colony was allocated to C++ Empire, and "B" indicates the Java Republic.

If there are several possible solutions, you could print just one of them.

If there is no solution, print "NO".

### 注意

This problem is special judged.

2
2
0 0
0 1
4
0 0
0 1
1 0
1 1

NO
YES
ABBA

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
struct point{
int x,y;
int index;
}p[1005],ans[1005],temp;
int n,top,visit[maxn];                             //top记录凸包上的点，visit作为标记，1代表A，0代表B

double across(point a,point b,point c)             //叉乘
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

double dis(point a,point b)                       //三点共线，求距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int cmp(point a,point b)                         //排序函数
{
if(!across(temp,a,b))
{
if(dis(temp,a)<dis(temp,b))
return 1;
else
return 0;
}

if(across(temp,a,b)>0)
return 1;
else
return 0;
}

void solve()                                    //得到凸包
{
int i,j,k;
top=1;
ans[0]=p[0];
ans[1]=p[1];

for(i=2;i<n;i++)
{
while(top>0&&across(ans[top-1],ans[top],p[i])<=0)
top--;
ans[++top]=p[i];
}
}

int main()
{
int i,j,k,t,cnt;
cin>>t;
while(t--)
{
cin>>n;
cnt=0;
temp.x=1005;
temp.y=1005;
mset(visit,0);
for(i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
p[i].index=i;
if(p[i].y<temp.y||(p[i].y==temp.y&&p[i].x<temp.x))
{
temp.x=p[i].x;
temp.y=p[i].y;
cnt=i;
}
}
if(n==1||n==2)
{
cout<<"NO"<<endl;
continue;
}
swap(p[cnt],p[0]);
sort(p+1,p+n,cmp);
solve();
if(n==3&&top==n-1)
{
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
if(top==n-1)
{
visit[ans[0].index]=1;
visit[ans[2].index]=1;
}
else if(top<n-1)
{
for(i=0;i<=top;i++)
visit[ans[i].index]=1;
}
for(i=0;i<n;i++)
{
if(visit[i])
cout<<"A";
else
cout<<"B";
}
cout<<endl;
}
return 0;
}

2
0

个人资料
• 访问：59699次
• 积分：2091
• 等级：
• 排名：千里之外
• 原创：154篇
• 转载：1篇
• 译文：0篇
• 评论：17条
联系方式
欢迎谈论交流：1245985209
博客专栏
 CodeForce题解 文章：39篇 阅读：16618
 ACM的进阶之路 文章：83篇 阅读：35701
文章分类