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>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define NL "\n"
#define SP " "
#define fmax(a, b) a = max(a, b)
#define fmin(a, b) a = min(a, b)
#define all(v) v.begin(), v.end()
const int mn = 1e6 + 1;
ll dp[mn][2][3];
bool A[mn], B[mn];
ll cost1 (int a, int b) {
if (b == 0 || a == b) return 0;
return 1;
}
ll cost2 (int a, int b) {
if (b == 2 || a == b) return 0;
return 1;
}
void update (int i, int j, int k) {
for (int j2 = 0; j2 < 2; j2++) {
for (int k2 = 0; k2 < 3; k2++)
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j2][k2] + cost1(j2, j) + cost2(k2, k));
}
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
char c; cin >> c;
if (c == '1') A[i] = 1;
}
for (int i = 1; i <= n; i++) {
char c; cin >> c;
if (c == '1') B[i] = 1;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) dp[i][j][k] = INT_MAX;
}
}
dp[0][0][2] = 0;
for (int i = 1; i <= n; i++) {
// xor
if (A[i] != B[i]) {
update(i, 1, 2);
update(i, 1, B[i]);
}
else update(i, 1, B[i]);
// not xor
if (A[i] != B[i]) update(i, 0, B[i]);
else {
update(i, 0, 2);
update(i, 0, B[i]);
}
}
ll ans = LLONG_MAX;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) ans = min(ans, dp[n][j][k]);
}
cout << ans;
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... |