Codeforces Round #651 (Div. 2) - E. Binary Subsequence Rotation(贪心)

58 篇文章 0 订阅
35 篇文章 1 订阅

Codeforces Round #651 (Div. 2) 全文见:https://blog.csdn.net/qq_43461168/article/details/112577099

E. Binary Subsequence Rotation

题意:给定一个01串,要变成目标串。可以选择任意一个子序列。然后顺时针移动一次。也就是最前面的到最后面,其实的移动到前一个元素的位置。问需要多少次。

思路:考虑贪心。首先,如果两个位置相等,那自然不用判断了。其次,选110或者001,这种也是非最优的选择,因为每次变化都有一个位置没有变,相当于浪费一次。所以显然是01交替出现。也就是只有两种序列可选 010101… 101010…,然后就遍历扫一遍,看这两种各最多需要多少个就行了。

AC代码:

#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define gcd __gcd
#define pb push_back
using namespace std;
const double eps = 1e-10;
const int mod = 1e9+7;
const int N = 1e6+7;
int n,m,k,t = 1,cas = 1;
int a[N],b[N],c[N];
int cnt1=0,cnt2=0,cnt3=0;

signed main(){
    string s1,s2;
    while(cin>>n){
        cin>>s1>>s2;
        int maxx1 = 0;
        int maxx2 = 0;
        int cnt0 = 0;
        int cnt1 = 0;
        for(int i = 0 ; i < n ; i ++){
            if(s1[i] == s2[i]) continue;
            if(s1[i] == '0'){
                if(cnt1) cnt1--;
                else cnt0++;
            }else{
                if(cnt0) cnt0--;
                else cnt1++;
            }
            maxx1 = max(maxx1,abs(cnt1));
            maxx2 = max(maxx2,abs(cnt0));
        }
        if(cnt1 || cnt0 )cout<<-1<<endl;
        else cout<<maxx1+maxx2<<endl;
    }
}

/**
**/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值