CF1705(思维,二进制)
作者:互联网
https://codeforces.com/contest/1705/problem/E
题意:给出01串s和t,问通过以下操作使s变成t的最小操作数。操作:s-1不同于s+1时,s取反。eg:110->100
场上直接模拟后,感觉直接模拟解决。但是比较麻烦,而且感觉很不对。
思路: 首先s[0]和s[n]是不变的,他们必须分别等于t[0],t[n]。仔细观察题意操作,出题人给这个题设定了比较强的性质。110->100,这样操作01+10的个数是不会变的。操作实际上是s`中的1和0交换位置。
从题目限制入手找性质,简化问题
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
#define PII pair<int, int>
//#define int long long
const int N = 2e5 + 5;
const int M = 2e7;
const int INF = 0x3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double PI = acos(-1.0);
int a[N], b[N];
void solve() {
int n; cin >> n;
vector<int> vea, veb;
string s1,s2; cin >> s1 >> s2;
for ( int i = 1; i <= n; ++ i ) a[i] = s1[i-1] - '0';
for ( int i = 1; i <= n; ++ i ) b[i] = s2[i-1] - '0';
if(a[0] != b[0] || a[1] != b[1]) {
cout << -1 << '\n'; return;
}
for ( int i = 2; i <= n; ++ i ){
if(a[i-1]^a[i]) vea.push_back(i-1);
}
for ( int i = 2; i <= n; ++ i ) {
if(b[i-1]^b[i]) veb.push_back(i-1);
}
if (vea.size()!=veb.size()){
cout << -1 << '\n'; return;
}
ll ans = 0;
for ( int i = 0; i < vea.size(); ++ i ) {
ans += abs(vea[i] - veb[i]);
}
cout << ans << '\n';
}
int main() {
int t; cin >> t;
while( t -- ) solve();
return 0;
}
标签:思维,const,二进制,cin,long,int,CF1705,操作,define 来源: https://www.cnblogs.com/muscletear/p/16609323.html