1. 程式人生 > >hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)

hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)

span per 則無 方案 plane ber != java res

#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

題目鏈接:

  http://hihocoder.com/problemset/problem/1582

題目大意:

  一個二維平面,上面有一些點,問是否存在01染色方案,使得0和1的點無法被一條直線區分開。

題目思路:

  【凸包】

  一開始題目看錯了以為是沿著網格的邊的折線。。

  首先可以知道n>3時只要滿足存在兩條線段,一條以2個0為端點,一條以2個1為端點,這兩條線段相交即可

  n=3時三角形無解,共線時中間的點為0,端點為1即可。

  n=2或者1時無解。

  可以求個凸包,然後判斷,如果有節點不在凸包上,那麽必有解,將不在凸包的點染為1,在的為0,即可。

  如果節點全在凸包上,若n>3,則取不相鄰的兩個節點染為1,其余為0即可。否則無解。

技術分享
  1 /****************************************************
  2 
  3     Author : Coolxxx
  4     Copyright 2017 by Coolxxx. All rights reserved.
  5     BLOG : http://blog.csdn.net/u010568270
  6 
  7 ****************************************************/
  8 #include<bits/stdc++.h>
  9 #pragma comment(linker,"/STACK:1024000000,1024000000")
 10 #define abs(a) ((a)>0?(a):(-(a)))
 11 #define lowbit(a) (a&(-a))
 12 #define sqr(a) ((a)*(a))
 13 #define mem(a,b) memset(a,b,sizeof(a))
 14 const double EPS=0.00001;
 15 const int J=10;
 16 const int MOD=1000000007;
 17 const int MAX=0x7f7f7f7f;
 18 const double PI=3.14159265358979323;
 19 const int N=104;
 20 using namespace std;
 21 typedef long long LL;
 22 double anss;
 23 LL aans;
 24 int cas,cass;
 25 int n,m,lll,ans;
 26 bool mark[N];
 27 class xxx
 28 {
 29 public:
 30     int x,y,num;
 31     xxx(){}
 32     xxx(int x,int y)
 33     {
 34         this->x=x;
 35         this->y=y;
 36     }
 37     int dis()
 38     {
 39         return sqr(x)+sqr(y);
 40     }
 41     int dis(xxx aa)
 42     {
 43         return sqr(x-aa.x)+sqr(y-aa.y);
 44     }
 45     xxx operator - (const xxx &bb)
 46     {
 47         return xxx(this->x-bb.x,this->y-bb.y);
 48     }
 49 }a[N],s[N];
 50 int Cross(xxx aa,xxx bb)
 51 {
 52     return aa.x*bb.y-aa.y*bb.x;
 53 }
 54 int Cross(xxx aa,xxx bb,xxx cc)
 55 {
 56     return Cross(bb-aa,cc-aa);
 57 }
 58 bool cmp1(xxx aa,xxx bb)
 59 {
 60     return aa.y!=bb.y?aa.y<bb.y:aa.x<bb.x;
 61 }
 62 bool cmp(xxx aa,xxx bb)
 63 {
 64     return Cross(a[1],aa,bb)!=0?(Cross(a[1],aa,bb)>0):(aa.dis(a[1])<bb.dis(a[1]));
 65 }
 66 bool judge()
 67 {
 68     int i;
 69     if(lll<n)
 70     {
 71         for(i=1;i<=lll;i++)
 72             mark[s[i].num]=1;
 73         return 1;
 74     }
 75     else if(n>3)
 76     {
 77         mark[s[1].num]=mark[s[3].num]=1;
 78         return 1;
 79     }
 80     else return 0;
 81 }
 82 int main()
 83 {
 84     #ifndef ONLINE_JUDGE
 85     freopen("1.txt","r",stdin);
 86 //    freopen("2.txt","w",stdout);
 87     #endif
 88     int i,j,k;
 89     int x,y,z;
 90     for(scanf("%d",&cass);cass;cass--)
 91 //    init();
 92 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
 93 //    while(~scanf("%d",&n))
 94     {
 95         mem(mark,0);lll=0;
 96         scanf("%d",&n);
 97         for(i=1;i<=n;i++)
 98         {
 99             scanf("%d%d",&a[i].x,&a[i].y);
100             a[i].num=i;
101         }
102         sort(a+1,a+1+n,cmp1);
103         sort(a+2,a+1+n,cmp);
104         for(i=1;i<=n;i++)
105         {
106             while(lll>1 && Cross(s[lll-1],a[i],s[lll])>=0)
107                 lll--;
108             s[++lll]=a[i];
109         }
110         
111         for(i=1;i<=lll;i++)s[i+lll]=s[i];
112         for(i=2,j=1;i<=lll;i++)
113             if(s[i].num<s[j].num)j=i;
114         for(i=j;i<j+lll;i++)
115             printf("%d ",s[i].num);
116         puts("");
117         
118         if(judge())
119         {
120             puts("YES");
121             for(i=1;i<=n;i++)
122                 printf("%c",mark[i]?A:B);
123             puts("");
124         }
125         else puts("NO");
126     }
127     return 0;
128 }
129 /*
130 //
131 
132 //
133 */
View Code

hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)