关闭

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

标签: acm-icpc几何凸包
923人阅读 评论(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;
}


2
0
查看评论

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A. Visiting Peking University

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A. Visiting Peking University
  • axuhongbo
  • axuhongbo
  • 2017-09-24 08:16
  • 565

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum-(线段树)

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum-(线段树) You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1...
  • sdau20163942
  • sdau20163942
  • 2017-09-23 16:49
  • 1034

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题解汇总 Territorial Dispute

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题解汇总 Territorial Dispute
  • axuhongbo
  • axuhongbo
  • 2017-09-24 09:02
  • 968

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 A. Visiting Peking University(模拟水题)

Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, …, n-1. He plans to spend m consecutive days(...
  • qq_37497322
  • qq_37497322
  • 2017-09-23 17:00
  • 472

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 G Bounce (规律)

Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a circle bouncing in a rectangle. As shown in the figure below...
  • Ever_glow
  • Ever_glow
  • 2017-09-24 09:36
  • 1017

ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 C (map模拟)

一开始想多了以为是AC自动机或者Tire之类的,后来发现直接用map,int>模拟即可。 就是想办法处理掉那些多余的空格和逗号句号,其实还是比较啰嗦,慢慢写就好 【代码】 /* *********************************************** Aut...
  • AngOn823
  • AngOn823
  • 2016-09-25 00:19
  • 642

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I题

题解思路:根据题意我们只需要维护区间最大的值和最小的值就够了,那么当最大的是小于0的那么就用最大的自身相乘,如果最小的是正数那么就是最小自身相乘,要是最小小于0最大大于0那么就是最大乘最小。 代码: #include #include #include #include #include...
  • a1214034447
  • a1214034447
  • 2017-09-27 17:07
  • 210

ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A

字典树 + dfs 题解: 题意: 有nn本书, 每本书的格式为CATEGORY 1/CATEGORY 2/..../CATEGORY n/BOOKNAME, 现在要重新格式化这些书的格式. 第nn个category前面需要有4(n-1)4(n−1)个空格, 如果这本书在第nn个ca...
  • AngOn823
  • AngOn823
  • 2016-09-25 00:15
  • 931

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

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 Territorial Dispute (凸包)计算几何
  • m0_37109329
  • m0_37109329
  • 2017-09-26 14:43
  • 98

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

题目链接:Territorial Dispute 题意:给出n个点,给出坐标,你可以把点编号为’A’或者’B’,问题时存不存在一种编号的方法使得编号完后不存在一条直线能把不同类型的点分到线的不同侧。如果不存在这种方法,输出“NO”。如果存在这种方法,输出“YES”,方法可能有多种,再输出其中一种方...
  • qq_33843429
  • qq_33843429
  • 2017-09-25 16:50
  • 50
    个人资料
    • 访问:59699次
    • 积分:2091
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:1篇
    • 译文:0篇
    • 评论:17条
    联系方式
    欢迎谈论交流:1245985209
    博客专栏