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

标签: acm-icpc 几何 凸包
970人阅读 评论(0) 收藏 举报
分类:

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


查看评论

Socket Programming with VB

 Socket Programming with VB我们在烟台项目的网络传输部分研究中涉及到了Winsock编程,经过较长时间的学习,做出了几个模型。本文是我在工作过程中的一些体会,不足之处请大家指...
  • ghj1976
  • ghj1976
  • 2001-08-19 17:39:00
  • 1552

H. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
  • axuhongbo
  • axuhongbo
  • 2017-09-09 19:06:48
  • 134

2017 ACM-ICPC 亚洲区(北京赛区)网络赛

题目9 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a...
  • i1020
  • i1020
  • 2017-09-23 20:59:47
  • 1182

2017 ACM-ICPC 网络赛 总结

整个九月,就在区域赛网络赛中度过了……8月有多校9月有网络赛,过的十分充实。坚持下来的这两个月,使我提升巨大!成功跨越了一个瓶颈期,现在终于有能力去攻克一些高难度的算法了。平时过得也很开心,最高兴的就...
  • lzc504603913
  • lzc504603913
  • 2017-09-24 21:47:11
  • 517

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
  • real_Rickys
  • real_Rickys
  • 2017-09-25 14:09:02
  • 215

2017 ICPC 南宁 M题 The Maximum Unreachable Node Set (二分图匹配)

The Maximum Unreachable Node Set时间限制: 20 Sec  内存限制: 128 MB提交: 51  解决: 8[提交][状态][讨论版][命题人:admin]题目描述I...
  • Ever_glow
  • Ever_glow
  • 2018-04-07 11:30:11
  • 53

计蒜客17115 Coin 数学 2017 ACM-ICPC 亚洲区(西安赛区)网络赛

题目链接:计蒜客17115 思路:=>在伯努利试验中,事件A出现的概率为P,求在n重伯努利试验中,事件A出现偶数次(包括出现0次)的概率和出现 奇数次的概率.这个应该很好想。 过程如下: 解释来自网上...
  • qq_34131212
  • qq_34131212
  • 2017-09-17 19:42:10
  • 481

hihoCoder - 1586 Minimum (2017 ACM-ICPC 亚洲区 (北京赛区) 网络赛 I)

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I #1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限...
  • lzc504603913
  • lzc504603913
  • 2017-09-23 18:55:20
  • 861

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph

In this problem, we will define a graph called star graph, and the question is to find the minimum d...
  • lzc504603913
  • lzc504603913
  • 2017-09-24 17:44:14
  • 532

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H. A Cache Simulator

Cache memories have been used widely in current microprocessor systems. In this problem, you are ask...
  • lzc504603913
  • lzc504603913
  • 2017-09-24 17:40:17
  • 660
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 6万+
    积分: 2253
    排名: 2万+
    联系方式
    欢迎谈论交流:1245985209
    博客专栏