Codeforces Round #651 (Div. 2) E - Binary Subsequence Rotation 思维

#include<map>
#include<queue>
#include<time.h>
#include<limits.h>
#include<cmath>
#include<ostream>
#include<iterator>
#include<set>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define mem(st) memset(st,0,sizeof st)
int read()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')             //判断正负
        flag=1;
    else if(ch>='0'&&ch<='9')           //得到完整的数
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef pair<double,double> pdd;
const int inf = 0x3f3f3f3f;
char str[1000020], tt[1000020];
signed main()
{
    int n;
    cin >> n >> str + 1 >> tt + 1;
    if(count(str + 1 , str + n + 1 , '1') != count(tt + 1 , tt + n + 1 , '1'))
        cout << -1 << '\n';
    else
    {
        int m = 0;
        rep(i , 1 , n)
        if(str[i] == tt[i]) //不一样的拿出来 
            continue;
        else
        {
            ++ m;
            str[m] = str[i] , tt[m] = tt[i];
        }
        int sum1 = 0, sum0 = 0, ans = 0;
        //从头开始, 
        rep(i , 1 , m)
        {
            if(str[i] == '1')
            //如果是1,那么久找距离 最近的0,转一下,那么也就相当于现在有的1整体往后移动 
            {
                sum1 ++;
                if(sum0) 
                    sum0 --;//如果存在整体往后移动的0 ,就说明之前移动过, 
                else 
                    ans ++;
            }
            else
            {
                sum0 ++;
                if(sum1) sum1 --;
                else ans ++;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

 

posted @ 2020-06-21 12:09  晴屿  阅读(232)  评论(0编辑  收藏  举报