Codeforces Round #545 (Div. 2) B. Circus(思维)

假如想象 提交于 2021-01-14 06:29:50

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
Note
In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 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 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.

解析:按照10,01,11,00把所有人分为abcd四种人
之后两层for循环
第一层枚举第一部分中扮演的有效人数
第二层枚举a种人的贡献
然后就可以依次确定四种人的个数了
之后输出即可

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=1e5+10;
int ar[N];
int ap[N];
int ac[N];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    string str1,str2;
    cin>>str1;
    cin>>str2;
    int a=0,b=0,c=0,d=0;
    int len=n/2;
    for(int i=0;i<n;i++){
        if(str1[i]=='1'&&str2[i]=='0') a++,ac[i]=1;
        else if(str1[i]=='0'&&str2[i]=='1') b++,ac[i]=2;
        else if(str1[i]=='1'&&str2[i]=='1') c++,ac[i]=3;
        else if(str1[i]=='0'&&str2[i]=='0') d++,ac[i]=4;

    }
    int A=-1,B=-1,C=-1,D=-1;
    for(int i=0;i<=len;i++){
        for(int j=0;j<=a&&j<=i;j++)
        {
            int a1,b1,c1,d1;
            a1=j;
            c1=i-j;
            if(a1<0||a1>a||c1<0||c1>c) continue;
            b1=b-(i-(c-c1));
            if(b1>b||b1<0) continue;
            d1=len-a1-b1-c1;
            if(d1>d||d1<0) continue;
            A=a1,B=b1,C=c1,D=d1;
            break;
        }
        if(A!=-1) break;
    }
    if(A==-1){
        cout<<-1<<endl;
        return 0;
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        if(ac[i]==1&&A!=0){
            ap[i]=1;
            A--;
        }
        if(ac[i]==2&&B!=0){
            ap[i]=1;
            B--;

        }
        if(ac[i]==3&&C!=0){
            ap[i]=1;
            C--;
        }
        if(ac[i]==4&&D!=0){
            ap[i]=1;
            D--;
        }
    }
    for(int i=0;i<n;i++){
        if(ap[i]) cout<<i+1<<' ';

    }
    cout<<endl;
    return 0;
}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!