[cf1138BCircus][枚举,列等式]
https://codeforc.es/contest/1138/problem/B
Polycarp is a head of a circus troupe. There are nn — an even number — artists in the troupe. It is known whether the ii-th artist can perform as a clown (if yes, then ci=1ci=1, otherwise ci=0ci=0), and whether they can perform as an acrobat (if yes, then ai=1ai=1, otherwise ai=0ai=0).
Split the artists into two performances in such a way that:
- each artist plays in exactly one performance,
- the number of artists in the two performances is equal (i.e. equal to n2n2),
- the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance.
The first line contains a single integer nn (2≤n≤50002≤n≤5000, nn is even) — the number of artists in the troupe.
The second line contains nn digits c1c2…cnc1c2…cn, the ii-th of which is equal to 11 if the ii-th artist can perform as a clown, and 00 otherwise.
The third line contains nn digits a1a2…ana1a2…an, the ii-th of which is equal to 11, if the ii-th artist can perform as an acrobat, and 00 otherwise.
Print n2n2 distinct integers — the indices of the artists that should play in the first performance.
If there are multiple answers, print any.
If there is no solution, print a single integer −1−1.
4 0011 0101
1 4
6 000000 111111
-1
4 0011 1100
4 3
8 00100101 01111100
1 2 3 6
In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 11 and 44 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 11. And the number of artists in the second performance who can perform as acrobats is 11 as well.
In the second example, the division is not possible.
In the third example, one of the possible divisions is as follows: in the first performance artists 33 and 44 should take part. Then in the first performance there are 22 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 22 as well.
题意:有四种类型的人,(01) (10) (00) (11),求一种分配方案,使两队人数相同,并且A队第一位为1的个数与B队第二位为1的个数相同 (n<=5000)
题解:直接枚举A队(10)和(01)的人数为 i , j ,设A队(11)为x,则可以得到一个等式 i+x = (n4-x) + (n1-j),即 x=(n4+n1-j-i)/2,所以可以解出相应的A队(11)的个数x以及(00)的个数 n/2-x-i-j
1 #include<iostream> 2 #include<cstdio> 3 #include<stack> 4 using namespace std; 5 #define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl; 6 typedef long long ll; 7 char ch[5005],ch2[5005]; 8 int bq[5005]; 9 int main(){ 10 int n; 11 scanf("%d",&n); 12 scanf("%s",ch+1); 13 scanf("%s",ch2+1); 14 int a,b,c,d; 15 a=b=c=d=0; 16 for(int i=1;i<=n;i++){ 17 if(ch[i]=='1'&&ch2[i]=='1')a++; 18 else if(ch[i]=='1'&&ch2[i]=='0')b++; 19 else if(ch[i]=='0'&&ch2[i]=='1')c++; 20 else if(ch[i]=='0'&&ch2[i]=='0')d++; 21 } 22 int a1,a2,a3,a4; 23 a1=a2=-1; 24 for(int i=0;i<=b;i++){ 25 int f=0; 26 for(int j=0;j<=c;j++){ 27 int x=i; 28 int y=c-j; 29 a3=(a+y-x)/2; 30 a4=n/2-x-j-a3; 31 if((a+y-x)%2==0&&a3>=0&&a3<=a&&a4>=0&&a4<=d){ 32 a1=i; 33 a2=j; 34 f=1; 35 break; 36 } 37 } 38 if(f)break; 39 } 40 if(a1==-1&&a2==-1){ 41 printf("-1\n"); 42 } 43 else{ 44 int tot=0; 45 for(int i=1;i<=n;i++){ 46 if(ch[i]=='1'&&ch2[i]=='1'&&a3){ 47 a3--; 48 bq[++tot]=i; 49 } 50 else if(ch[i]=='1'&&ch2[i]=='0'&&a1){ 51 a1--; 52 bq[++tot]=i; 53 } 54 else if(ch[i]=='0'&&ch2[i]=='1'&&a2){ 55 a2--; 56 bq[++tot]=i; 57 } 58 else if(ch[i]=='0'&&ch2[i]=='0'&&a4){ 59 a4--; 60 bq[++tot]=i; 61 } 62 } 63 for(int i=1;i<=n/2;i++){ 64 printf("%d",bq[i]); 65 char cc=(i==n/2)?'\n':' '; 66 printf("%c",cc); 67 } 68 } 69 return 0; 70 }