#include <bits/stdc++.h>
using namespace std;
int 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] = 1e9;
if(B[0] == 0) dp[0][1][0] = 1;
else dp[0][1][0] = 1e9;
if(B[0] == 1) dp[0][2][0] = 1;
else dp[0][2][0] = 1e9;
if(A[0] != B[0]) dp[0][0][1] = 1;
else dp[0][0][1] = 1e9;
dp[0][1][1] = 2; dp[0][2][1] = 2;
for(int i = 1; i < N; i++) {
int minprev = 1e9;
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] = 1e9;
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] = 1e9;
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] = 1e9;
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] = 1e9;
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 = INT_MAX;
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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
62 ms |
27732 KB |
Output is correct |
8 |
Correct |
59 ms |
27768 KB |
Output is correct |
9 |
Correct |
61 ms |
27724 KB |
Output is correct |
10 |
Correct |
60 ms |
27804 KB |
Output is correct |
11 |
Correct |
61 ms |
27768 KB |
Output is correct |
12 |
Correct |
61 ms |
27696 KB |
Output is correct |
13 |
Correct |
66 ms |
27896 KB |
Output is correct |
14 |
Correct |
61 ms |
27820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |