官方题解

cookiebus 2024-04-20 13:20:12 2024-04-20 13:20:18 5 返回题目

sol-1.png

sol-2.png

参考代码:

#include <bits/stdc++.h>
using namespace std;
int n, k;
long long ans = 0;
char a[1000005], b[1000005];
multiset<pair<int, int>> s[2];
int main() {
    cin >> n >> k >> a >> b;
    for (int i = 0; i < n; i++)
        if (a[i] != b[i]) {
            int x = a[i] - '0';
            if (s[!x].empty())
                s[x].insert(make_pair(i % k, i));
            else {
                auto it = s[!x].lower_bound(make_pair(i % k, 0));
                if (it == s[!x].end())
                    it = s[!x].begin();
                ans += (i - (it->second) - 1) / k + 1, s[!x].erase(it);
            }
        }
    cout << ans;
    return 0;
}
{{ vote && vote.total.up }}