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

在2333年,C++帝国与Java共和国争夺火星殖民地,邪恶程序员David计划通过巧妙分配殖民地引发两国间无法划定国界的领土争端,从而导致战争。

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

#1582 : 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 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个点。判断是否能将n个点分给A和B,使不存在能够用一条直线将n各点分成只有A和B的两部分。

思路:

分类讨论。

当n<=2时 ,肯定不符合要求。

当n==3时,当且仅当三个点在一条直线上时符合要求。直线两端为一类,夹在中间的点为一类。

当n>=4时,肯定存在符合要求的分法。当取四个点存在两条线段相交时,每条线段各成一类,这四个点之外的其他点随便分。

当不存在两条线段相交时,一定存在三个点成三角形,另外一个点被三角形包围。 此时三角形三个点为一类,中间点为另一类,其他点随便分。

即枚举四个点的三种线段组成方式,判断是否相交;和 枚举四个点的三种三角形组成方式,判断点是否包含。

 

这里点是否在三角形内的判断方式。是通过判断点是否在三条边同一侧。先算线的直线方程ax+by+c=0;

再算d=ax+by+c,

d < 0, 点在直线左侧

d = 0,点在直线上

d > 0, 点在直线右侧

只要乘积>=0即在同一侧

 

代码:

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
struct node{
    double x,y;
    int id;
    int type;
}Node[110];
struct Llineseg{
    node a,b;
};
const double eps = 1e-10;
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
double dis(int a,int b)
{
    return sqrt((Node[a].x-Node[b].x)*(Node[a].x-Node[b].x)+(Node[a].y-Node[b].y)*(Node[a].y-Node[b].y));
}
bool inter(node a,node b,node c,node d)
{
    if(min(a.x,b.x)>max(c.x,d.x)||
       min(a.y,b.y)>max(c.y,d.y)||
       min(c.x,d.x)>max(a.x,b.x)||
       min(c.y,d.y)>max(a.y,b.y))return 0;
    double h,i,j,k;

    h = (b.x-a.x)*(c.y-a.y)-(b.y - a.y)*(c.x - a.x);
    i = (b.x-a.x)*(d.y-a.y)-(b.y - a.y)*(d.x - a.x);
    j = (d.x-c.x)*(a.y-c.y)-(d.y - c.y)*(a.x - c.x);
    k = (d.x-c.x)*(b.y-c.y)-(d.y - c.y)*(b.x - c.x);
    return h*i<=eps&&j*k<=eps;
}
double toLine(node a,node b,node c)
{
    double f1 = c.y-b.y;
    double f2 = b.x-c.x;
    double f3 = c.x*b.y - b.x*c.y;
    assert(fabs(f1)>eps||fabs(f2)>eps);
    return f1*a.x+f2*a.y+f3;
}
bool contain(node a,node b,node c,node d)
{
    double d1 = toLine(d,a,b);
    double d2 = toLine(d,b,c);
    if(d1*d2<0)return false;
    double d3 = toLine(d,c,a);
    if(d2*d3<0)return false;
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&Node[i].x,&Node[i].y);
            Node[i].id = i;
            Node[i].type=0;
        }
        if(n<=2)
        {
            printf("NO\n");
            continue;
        }
        if(n==3)//共线
        {
            if((Node[1].y-Node[0].y)*(Node[2].x-Node[1].x)==(Node[2].y-Node[1].y)*(Node[1].x-Node[0].x))
            {
                printf("YES\n");
                if(dis(0,1)+dis(0,2)==dis(1,2))
                    printf("ABB\n");
                else if(dis(1,0)+dis(1,2)==dis(0,2))
                    printf("ABA\n");
                else printf("AAB\n");
            }
            else printf("NO\n");

        }
        else{
            printf("YES\n");
            //两线段相交
            if(inter(Node[0],Node[1],Node[2],Node[3]))
            {
                Node[0].type=1;
                Node[1].type=1;
            }
            else if(inter(Node[0],Node[2],Node[1],Node[3]))
            {
                Node[0].type=1;
                Node[2].type=1;
            }
            else if(inter(Node[0],Node[3],Node[1],Node[2]))
            {
                Node[0].type=1;
                Node[3].type=1;
            }
            //两线段不相交,一个点在三角形内部
            else if(contain(Node[0],Node[1],Node[2],Node[3]))
            {
                Node[3].type=1;
            }
            else if(contain(Node[0],Node[1],Node[3],Node[2]))
            {
                Node[2].type=1;
            }
            else if(contain(Node[0],Node[3],Node[2],Node[1]))
            {
                Node[1].type=1;
            }
            else Node[0].type=1;

            for(int i=0;i<n;i++)
            {
                if(Node[i].type)printf("A");
                else printf("B");
            }
            printf("\n");

        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值