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;
}
}
/**
**/