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

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

#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");

        }
    }
}

 

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值