B. Circus

link

[https://codeforces.com/problemset/problem/1138/B]

The meaning of problems

Give you two strings 0,1 that, if you can elect half of the string, at the same time take.
The first string such that the current number is equal to 1, the number is not selected from the remaining second string 1.

analysis

First of all I was really stupid, because I have been discussing classification where the results like a long time, it will not work.
So this time we look at the data n <= 5000, which is n * n can be resolved. How violence?
This problem is mathematical skills of your junior high school embodies. A number of statistical 00,01,10,11, B, C, D;
we assume that the number 00, is selected from A, B, C, D;
A + B + C + D = n / 2; A + B + C + D = n;
we enumerate a, then the meaning of the questions need to meet

This really is a question of mathematics ah, but also my reaction is not sensitive to the complexity, can sometimes be violent, it will be violence, because it is not easy to make mistakes more stable.
The complexity of the analysis, which I still regard dishes.

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e3+10;

char s[N],t[N];
int main(){
    int n;
    while(~scanf("%d",&n)){
    
        scanf("%s",s+1);
        scanf("%s",t+1);
        int a,b,c,d,A=0,B=0,D=0,C=0;
        for(int i=1;i<=n;i++)
        if(s[i]=='0'&&t[i]=='1')
        B++;
        else if(s[i]=='1'&&t[i]=='0')
        C++;
        else if(s[i]=='1'&&t[i]=='1')
        D++;
        A=n-(B+D+C);
        //cout<<A<<' '<<B<<' '<<C<<' '<<D<<endl;
        bool flag=0;
        for(a=0;a<=A&&a<=n/2;a++)
        {
            d=B+D+a-n/2;
            if(d>=0&&d<=D&&(a+d)<=n/2){
                for(b=0;b<=B&&(a+b+d)<=n/2;b++){
                    c=n/2-(a+b+d);
                    if(c>=0&&c<=C){
                        flag=1; break;
                    }
                }
            }
            if(flag) break;
        }
        if(!flag) puts("-1");
        else {
            //cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
            for(int i=1;i<=n;i++)
            if(s[i]=='0'){
                if(t[i]=='0'&&a){
                    printf("%d ",i);
                    a--;
                }
                if(t[i]=='1'&&b){
                    printf("%d ",i);
                    b--;
                }
            }
            else{
                if(t[i]=='0'&&c){
                    printf("%d ",i);
                    c--;
                }
                if(t[i]=='1'&&d){
                    printf("%d ",i);
                    d--;
                }
            }
            puts("");
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/mch5201314/p/10974085.html