#include <bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main() {
int N; cin >> N; string A, B; cin >> A >> B;
int dp[N][3][2]; // idx, got 0 and got 1, got flip
if(A[0] == B[0]) dp[0][0][0] = 0;
else dp[0][0][0] = 1e15;
if(B[0] == 0) dp[0][1][0] = 1;
else dp[0][1][0] = 1e15;
if(B[0] == 1) dp[0][2][0] = 1;
else dp[0][2][0] = 1e15;
if(A[0] != B[0]) dp[0][0][1] = 1;
else dp[0][0][1] = 1e15;
dp[0][1][1] = 2; dp[0][2][1] = 2;
for(int i = 1; i < N; i++) {
int minprev = 1e15;
for(int j = 0; j < 3; j++) for(int k = 0; k < 2; k++) {
minprev = min(minprev, dp[i - 1][j][k]);
}
if(A[i] == B[i]) dp[i][0][0] = minprev;
else dp[i][0][0] = 1e15;
if(B[i] == '0') dp[i][1][0] = min(minprev + 1, min(dp[i - 1][1][0], dp[i - 1][1][1]));
else dp[i][1][0] = 1e15;
if(B[i] == '1') dp[i][2][0] = min(minprev + 1, min(dp[i - 1][2][0], dp[i - 1][2][1]));
else dp[i][2][0] = 1e15;
if(A[i] != B[i]) dp[i][0][1] = min(minprev + 1, min(dp[i - 1][0][1], min(dp[i - 1][1][1], dp[i - 1][2][1])));
else dp[i][0][1] = 1e15;
dp[i][1][1] = min(minprev + 2, min(dp[i - 1][1][0], dp[i - 1][1][1]) + 1);
dp[i][1][1] = min(dp[i][1][1], min(min(dp[i - 1][0][1], dp[i - 1][1][1]), dp[i - 1][2][1]) + 1);
if(B[i] == B[i - 1]) {
dp[i][1][1] = min(dp[i][1][1], dp[i - 1][1][1]);
}
dp[i][2][1] = min(minprev + 2, min(dp[i - 1][2][0], dp[i - 1][2][1]) + 1);
dp[i][2][1] = min(dp[i][2][1], min(min(dp[i - 1][0][1], dp[i - 1][1][1]), dp[i - 1][2][1]) + 1);
if(B[i] == B[i - 1]) {
dp[i][2][1] = min(dp[i][2][1], dp[i - 1][2][1]);
}
// for(int k = 0; k < 3; k++) for(int j = 0; j < 2; j++) printf("%d %d %d %d\n", i, k, j, dp[i][k][j]);
}
int ans = 1e15;
for(int i = 0; i < 3; i++) for(int j = 0; j < 2; j++) ans = min(ans, dp[N - 1][i][j]);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
68 ms |
49296 KB |
Output is correct |
8 |
Correct |
68 ms |
49236 KB |
Output is correct |
9 |
Correct |
68 ms |
49344 KB |
Output is correct |
10 |
Correct |
69 ms |
49236 KB |
Output is correct |
11 |
Correct |
94 ms |
49244 KB |
Output is correct |
12 |
Correct |
69 ms |
49256 KB |
Output is correct |
13 |
Correct |
68 ms |
49316 KB |
Output is correct |
14 |
Correct |
68 ms |
49340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |