2017北京网络赛 && hihocoder 1582 Territorial Dispute(凸包)

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 colonies ofdifferent 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

题意:给你n个二维平面上的点,问你怎么分配他们的归属(A或B),使得一条直线不能把这堆点分成两个部分,每个部分中都是A或者都是B。

如果可以使得他不能分成两个部分输出YES,并输出分配方案,否则输出NO

思路:

1、小于等于2个点的情况肯定都是NO

2、等于3个点的情况,如果在一条直线上是YES,不在就是NO

3、大于3个点求一波凸包,在凸包上的是A,不在凸包上的是B


#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;

const double eps = 1e-8;

struct Point
{
    int x,y;
    Point(){}
    Point(int _x,int _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    int operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    int operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
};
bool cmp(Point a,Point b)
{
    if(a.x != b.x)
        return a.x < b.x;
    return a.y < b.y;
}

vector<Point> convex_hull(vector<Point> p)
{
    sort(p.begin(),p.end(),cmp);
    int k = 0;
    vector<Point> v(2*p.size());
    for(int i=0;i<p.size();i++)
    {
        while(k > 1 && ((v[k-1] - v[k-2])^(p[i]-v[k-1])) <= 0)
            k--;
        v[k++] = p[i];
    }
    for(int i=p.size()-2,t=k;i>=0;i--)
    {
        while(k > t && ((v[k-1] - v[k-2])^(p[i]-v[k-1])) <= 0)
            k--;
        v[k++] = p[i];
    }
    v.resize(k-1);

    return v;
}

vector<Point> p,v;

int c[1100][1100];
Point a[110];

int main(void)
{
    int T,n,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        p.clear();
        v.clear();
        memset(c,-1,sizeof(c));
        for(i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[i] = Point(x,y);
            p.push_back(Point(x,y));
        }
        v = convex_hull(p);
        if(n < 3 || n == 3 && v.size() == 3)
        {
            printf("NO\n");
            continue;
        }
        if(v.size() == n)
        {
            int t = 0;
            for(i=0;i<v.size();i++)
            {
                int x = v[i].x;
                int y = v[i].y;
                c[x][y] = t;
                t ^= 1;
            }
        }
        else
        {
            for(i=0;i<v.size();i++)
            {
                int x = v[i].x;
                int y = v[i].y;
                c[x][y] = 0;
            }
            for(i=1;i<=n;i++)
            {
                int x = a[i].x;
                int y = a[i].y;
                if(c[x][y] == -1)
                    c[x][y] = 1;
            }
        }
        printf("YES\n");
        for(i=1;i<=n;i++)
        {
            int x = a[i].x;
            int y = a[i].y;
            if(c[x][y] == 0)
                printf("A");
            else
                printf("B");
        }
        printf("\n");
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值