B. Circus
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp is a head of a circus troupe. There are n — an even number — artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then ci=1, otherwise ci=0), and whether they can perform as an acrobat (if yes, then ai=1, otherwise ai=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 n2),
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.
Input
The first line contains a single integer n (2≤n≤5000, n is even) — the number of artists in the troupe.
The second line contains n digits c1c2…cn, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise.
The third line contains n digits a1a2…an, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise.
Output
Print n2 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.
Examples
inputCopy
4
0011
0101
outputCopy
1 4
inputCopy
6
000000
111111
outputCopy
-1
inputCopy
4
0011
1100
outputCopy
4 3
inputCopy
8
00100101
01111100
outputCopy
1 2 3 6
题解:一共有四种演员a,b,c,d(分别是只能演clown ,只能演acrobat,两种都会,两种都不会),统计这四种演员的数目aa,bb,cc,dd,然后标记每种演员的类别(1,2,3,4)(等一下输出要用)。假设第一场需要四种演员的数目分别为na,nb,nc,nd。那么根据题意就有 na+nb+nc+nd=n/2, 而且na+nc=cc-nc+bb-nb;再根据关系式对na,nc进行暴力就行,因为na,nc确定了,nb,nd就也确定的。然后如果nc,nd能符合条件的话,那么就直接跳出循环输出就行,如果循环完了还没有答案,就输出-1;
下面是AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
char a[5005];
char b[5005];
int c[5005];
int n;
int aa, bb, cc,dd;
int na, nb, nc,nd;
aa = bb = cc = dd = 0;
na = nb = nc = nd = 0;
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)cin >> b[i];
//统计数目
for (int i = 1; i <= n; i++) {
if (a[i] == '1' && b[i] == '1') {
c[i] = 3; cc++;
}
if (a[i] == '1' && b[i] == '0') {
c[i] = 1; aa++;
}
if (a[i] == '0' && b[i] == '1') {
c[i] = 2; bb++;
}
if (a[i] == '0'&&b[i] == '0') {
c[i] = 4; dd++;
}
}
//根据关系式我们可以得出这个结论
if (aa > n / 2 || bb > n / 2 ) {
printf("-1"); return 0;
}
//暴力大法妙
for (int j = 0; j <= aa; j++) {
for (int jj = 0; jj <= cc; jj++) {
na = j;
nc = jj;
nb = bb +cc - j - 2 * jj;
nd = n / 2 - na - nb - nc;
if (nb >= 0 && nb <= bb && nd >= 0 && nd <= dd)goto a;
}
}
printf("-1\n"); return 0;
a:
//得到na,nb,nc,nd直接输出就行
for (int i = 1; i <= n; i++) {
if (c[i] == 1 && na) {
printf("%d ", i); na--;
}
else if (c[i] == 3 && nc) {
printf("%d ", i); nc--;
}
else if (c[i] == 4 && nd) {
printf("%d ", i); nd--;
}
else if (c[i] == 2 && nb) {
printf("%d ", i); nb--;
}
}
return 0;
}
总结:这是一个偏数学的模拟题