This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
int dp[2000][2000][2];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
if (n > 500) {
std::string a, b;
std::cin >> a >> b;
int ans = 0;
for (int i = 0; i < n; i++) {
if (b[i] != '1') {
continue;
}
while (i + 1 < n && b[i + 1] == '1') {
i++;
}
ans++;
}
std::cout << ans << "\n";
} else {
std::string a[2], b;
std::cin >> a[0] >> b;
a[1] = a[0];
for (int i = 0; i < n; i++) {
a[1][i] ^= 1;
}
auto cost = [&](char c, int l, int r) {
int ans = 0;
for (int i = l; i <= r; i++) {
if (c != b[i]) {
while (i + 1 <= r && b[i + 1] != c) {
i++;
}
ans++;
}
}
return ans;
};
memset(dp, -1, sizeof dp);
std::function<int(int, int, int)> calc = [&](int l, int r, int f) {
if (l == r) {
return int(a[f][l] != b[l]);
}
int &ret = dp[l][r][f];
if (~ret) {
return ret;
}
ret = 1e9;
ret = std::min(ret, calc(l, r, f ^ 1) + 1);
ret = std::min(ret, cost('0', l, r) + 1);
ret = std::min(ret, cost('1', l, r) + 1);
for (int i = l; i < r; i++) {
ret = std::min(ret, calc(l, i, f) + calc(i + 1, r, f));
}
return ret;
};
std::cout << calc(0, n - 1, 0) << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |