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

本文探讨了一种特殊的殖民地分配问题,在火星上如何分配N个殖民地以避免通过直线划分成两个不同国家的问题。提供了算法思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Territorial Dispute

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

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

一直在水这个题,最后没时间了,还是没A到,其实想明白了的话,题目很简单的。

总的来说,就是给你N个点,这些点可以标记为A或者B,你是否有一种标记方法使得无法用一条直线来划分A和B,也就是无法使直线两边分别为A和B。

首先一个点,两个点都无法得到这样的直线,三个点的时候,若三点不共线,也无法得到,但如果三点共线就可以得到这样的点,即ABA或BAB,此时要根据距离标记。当N大于等于4时,肯定有解。不妨做一个凸包,把凸包上的点全部标记为A,凸包内的点全部标记为B。若是所有的点都在凸包上,那么只需要隔标记第一个点跟第三个点为A就可以。最后输出答案。

代码实现:

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值