题面如下:
思路 or 题解
一共有两组操作
选择 a 串 两个不同的位置,交换一下,代价是abs(i - j)
选择 a 串 一个位置进行反转(异或1)
1 - > 0
0 -> 1
可以发现什么?
如果使用第一个操作,那么代价只能是 1
用反证法:
如果代价 大于 1
我们可以完全只用 操作2 进行, 代价是2, 不会比 操作1 要差
所以可以证明 只有代价为 1 的时候才会使用 操作1
所以我们记录两串不同的位置
进行贪心
AC代码如下
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <bitset>
#include <set>
#include <random>
#include <ctime>
#include <queue>
#include <stack>
#include <climits>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
//#define int long long
//#define ll long long
#define PII pair<int, int>
#define px first
#define py second
typedef std::mt19937 Random_mt19937;
Random_mt19937 rnd(time(0));
using namespace std;
const int N = 100009;
int n;
string a, b;
vector<int> v1, v0;
void solve()
{
cin >> n >> a >> b;
a = '#' + a, b = '#' + b;
for (int i = 1; i <= n; i++)
{
if (a[i] == b[i])
continue;
if (a[i] == '1')
v1.push_back(i);
else
v0.push_back(i);
}
int ans = 0;
while (v0.size() && v1.size())
{
int x = v0.back(), y = v1.back();
v0.pop_back(), v1.pop_back();
if (abs(x - y) == 1)
ans++;
else
{
ans++;
if (x > y)
v1.push_back(y);
else
v0.push_back(x);
}
}
ans += v1.size() + v0.size();
cout << ans << '\n';
}
int main()
{
buff;
solve();
}