#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
const int MAXN = 1e6 + 10;
const int INF = 1e8;
int N;
string As, Bs;
int a[MAXN], b[MAXN];
int dp[MAXN][3][2];
int main() {
cin >> N;
cin >> As >> Bs;
As = ' ' + As;
Bs = ' ' + Bs;
for (int i = 1; i <= N; i++) {
a[i] = As[i] - '0';
b[i] = Bs[i] - '0';
}
a[0] = b[0] = 0; // initialize to be the same so no toggle needed
for (int i = 0; i < MAXN; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
dp[i][j][k] = INF;
}
}
}
dp[0][0][0] = 0;
for (int i = 0; i < N; i++) {
// case 1: nothing
int tog = a[i] ^b[i];
int cCost = dp[i][0][0];
if (cCost != INF) {
// case 1a: continue nothing
int nTog = a[i + 1] ^b[i + 1];
int nCost = cCost + (tog < nTog);
dp[i + 1][0][0] = min(dp[i + 1][0][0], nCost);
// case 1b: add a set
for (int nSet = 0; nSet < 2; nSet++) {
int nTog = nSet ^b[i + 1];
int nCost = cCost + 1 + (tog < nTog);
dp[i + 1][1][nSet] = min(dp[i + 1][1][nSet], nCost);
}
}
// case 2: 1 set
for (int cSet = 0; cSet < 2; cSet++) {
int tog = cSet ^b[i];
int cCost = dp[i][1][cSet];
if (cCost != INF) {
// case 2a: continue/change cSet
for (int nSet = 0; nSet < 2; nSet++) {
int nTog = nSet ^b[i + 1];
int nCost = cCost + (cSet ^ nSet) + (tog < nTog);
dp[i + 1][1][nSet] = min(dp[i + 1][1][nSet], nCost);
}
// case 2b: stop set
int nTog = a[i + 1] ^b[i + 1];
int nCost = cCost + (tog < nTog);
dp[i + 1][0][0] = min(dp[i + 1][0][0], nCost);
// case 2c: add second set that is opposite of cSet
nTog = (cSet ^ 1) ^ b[i + 1];
nCost = cCost + 1 + (tog < nTog);
dp[i + 1][2][cSet] = min(dp[i + 1][2][cSet], nCost);
}
}
// case 3: 2 sets, cSet is the the base set
for (int cSet = 0; cSet < 2; cSet++) {
int tog = cSet ^1 ^b[i];
int cCost = dp[i][2][cSet];
if (cCost != INF) {
// case 2a: continue cSet
int nTog = cSet ^1 ^b[i + 1];
int nCost = cCost + (tog < nTog);
dp[i + 1][2][cSet] = min(dp[i + 1][2][cSet], nCost);
// case 2b: stop second set
nTog = cSet ^ b[i + 1];
nCost = cCost + (tog < nTog);
dp[i + 1][1][cSet] = min(dp[i + 1][1][cSet], nCost);
}
}
}
int ans = INF;
assert(dp[N][0][1] == INF);
// case 1: no set
ans = min(ans, dp[N][0][0]);
// case 2: 1 set
ans = min(ans, dp[N][1][0]);
ans = min(ans, dp[N][1][1]);
// case 2: 2 set
ans = min(ans, dp[N][2][0]);
ans = min(ans, dp[N][2][1]);
// cout << dp[7][0][0] << endl;
// cout << dp[8][0][0] << endl;
assert(ans >= 0);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
18 ms |
23800 KB |
Output is correct |
3 |
Correct |
18 ms |
23800 KB |
Output is correct |
4 |
Correct |
18 ms |
23800 KB |
Output is correct |
5 |
Correct |
18 ms |
23800 KB |
Output is correct |
6 |
Correct |
18 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
11 |
Correct |
18 ms |
23800 KB |
Output is correct |
12 |
Correct |
18 ms |
23800 KB |
Output is correct |
13 |
Correct |
18 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
18 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
18 ms |
23800 KB |
Output is correct |
18 |
Correct |
18 ms |
23800 KB |
Output is correct |
19 |
Correct |
18 ms |
23800 KB |
Output is correct |
20 |
Correct |
18 ms |
23800 KB |
Output is correct |
21 |
Correct |
18 ms |
23800 KB |
Output is correct |
22 |
Correct |
18 ms |
23800 KB |
Output is correct |
23 |
Correct |
18 ms |
23800 KB |
Output is correct |
24 |
Correct |
18 ms |
23800 KB |
Output is correct |
25 |
Correct |
18 ms |
23804 KB |
Output is correct |
26 |
Correct |
17 ms |
23800 KB |
Output is correct |
27 |
Correct |
18 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23800 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
18 ms |
23800 KB |
Output is correct |
31 |
Correct |
18 ms |
23800 KB |
Output is correct |
32 |
Correct |
18 ms |
23800 KB |
Output is correct |
33 |
Correct |
18 ms |
23800 KB |
Output is correct |
34 |
Correct |
18 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
18 ms |
23800 KB |
Output is correct |
3 |
Correct |
18 ms |
23800 KB |
Output is correct |
4 |
Correct |
18 ms |
23800 KB |
Output is correct |
5 |
Correct |
18 ms |
23800 KB |
Output is correct |
6 |
Correct |
18 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
11 |
Correct |
18 ms |
23800 KB |
Output is correct |
12 |
Correct |
18 ms |
23800 KB |
Output is correct |
13 |
Correct |
18 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
18 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
18 ms |
23800 KB |
Output is correct |
18 |
Correct |
18 ms |
23800 KB |
Output is correct |
19 |
Correct |
18 ms |
23800 KB |
Output is correct |
20 |
Correct |
18 ms |
23800 KB |
Output is correct |
21 |
Correct |
18 ms |
23800 KB |
Output is correct |
22 |
Correct |
18 ms |
23800 KB |
Output is correct |
23 |
Correct |
18 ms |
23800 KB |
Output is correct |
24 |
Correct |
18 ms |
23800 KB |
Output is correct |
25 |
Correct |
18 ms |
23804 KB |
Output is correct |
26 |
Correct |
17 ms |
23800 KB |
Output is correct |
27 |
Correct |
18 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23800 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
18 ms |
23800 KB |
Output is correct |
31 |
Correct |
18 ms |
23800 KB |
Output is correct |
32 |
Correct |
18 ms |
23800 KB |
Output is correct |
33 |
Correct |
18 ms |
23800 KB |
Output is correct |
34 |
Correct |
18 ms |
23800 KB |
Output is correct |
35 |
Correct |
18 ms |
23800 KB |
Output is correct |
36 |
Correct |
18 ms |
23800 KB |
Output is correct |
37 |
Correct |
18 ms |
23800 KB |
Output is correct |
38 |
Correct |
19 ms |
23800 KB |
Output is correct |
39 |
Correct |
18 ms |
23800 KB |
Output is correct |
40 |
Correct |
18 ms |
23800 KB |
Output is correct |
41 |
Correct |
18 ms |
23800 KB |
Output is correct |
42 |
Correct |
19 ms |
23800 KB |
Output is correct |
43 |
Correct |
19 ms |
23800 KB |
Output is correct |
44 |
Correct |
18 ms |
23800 KB |
Output is correct |
45 |
Correct |
18 ms |
23800 KB |
Output is correct |
46 |
Correct |
18 ms |
23800 KB |
Output is correct |
47 |
Correct |
19 ms |
23800 KB |
Output is correct |
48 |
Correct |
19 ms |
23800 KB |
Output is correct |
49 |
Correct |
18 ms |
23800 KB |
Output is correct |
50 |
Correct |
18 ms |
23800 KB |
Output is correct |
51 |
Correct |
18 ms |
23800 KB |
Output is correct |
52 |
Correct |
18 ms |
23800 KB |
Output is correct |
53 |
Correct |
19 ms |
23800 KB |
Output is correct |
54 |
Correct |
18 ms |
23800 KB |
Output is correct |
55 |
Correct |
19 ms |
23800 KB |
Output is correct |
56 |
Correct |
18 ms |
23800 KB |
Output is correct |
57 |
Correct |
18 ms |
23800 KB |
Output is correct |
58 |
Correct |
17 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
18 ms |
23800 KB |
Output is correct |
3 |
Correct |
18 ms |
23800 KB |
Output is correct |
4 |
Correct |
18 ms |
23800 KB |
Output is correct |
5 |
Correct |
18 ms |
23848 KB |
Output is correct |
6 |
Correct |
18 ms |
23800 KB |
Output is correct |
7 |
Correct |
138 ms |
33588 KB |
Output is correct |
8 |
Correct |
138 ms |
33976 KB |
Output is correct |
9 |
Correct |
141 ms |
35500 KB |
Output is correct |
10 |
Correct |
140 ms |
35504 KB |
Output is correct |
11 |
Correct |
139 ms |
35516 KB |
Output is correct |
12 |
Correct |
139 ms |
35508 KB |
Output is correct |
13 |
Correct |
140 ms |
35512 KB |
Output is correct |
14 |
Correct |
140 ms |
35512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
18 ms |
23800 KB |
Output is correct |
3 |
Correct |
18 ms |
23800 KB |
Output is correct |
4 |
Correct |
18 ms |
23800 KB |
Output is correct |
5 |
Correct |
18 ms |
23800 KB |
Output is correct |
6 |
Correct |
18 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
11 |
Correct |
18 ms |
23800 KB |
Output is correct |
12 |
Correct |
18 ms |
23800 KB |
Output is correct |
13 |
Correct |
18 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
18 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
18 ms |
23800 KB |
Output is correct |
18 |
Correct |
18 ms |
23800 KB |
Output is correct |
19 |
Correct |
18 ms |
23800 KB |
Output is correct |
20 |
Correct |
18 ms |
23800 KB |
Output is correct |
21 |
Correct |
18 ms |
23800 KB |
Output is correct |
22 |
Correct |
18 ms |
23800 KB |
Output is correct |
23 |
Correct |
18 ms |
23800 KB |
Output is correct |
24 |
Correct |
18 ms |
23800 KB |
Output is correct |
25 |
Correct |
18 ms |
23804 KB |
Output is correct |
26 |
Correct |
17 ms |
23800 KB |
Output is correct |
27 |
Correct |
18 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23800 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
18 ms |
23800 KB |
Output is correct |
31 |
Correct |
18 ms |
23800 KB |
Output is correct |
32 |
Correct |
18 ms |
23800 KB |
Output is correct |
33 |
Correct |
18 ms |
23800 KB |
Output is correct |
34 |
Correct |
18 ms |
23800 KB |
Output is correct |
35 |
Correct |
18 ms |
23800 KB |
Output is correct |
36 |
Correct |
18 ms |
23800 KB |
Output is correct |
37 |
Correct |
18 ms |
23800 KB |
Output is correct |
38 |
Correct |
19 ms |
23800 KB |
Output is correct |
39 |
Correct |
18 ms |
23800 KB |
Output is correct |
40 |
Correct |
18 ms |
23800 KB |
Output is correct |
41 |
Correct |
18 ms |
23800 KB |
Output is correct |
42 |
Correct |
19 ms |
23800 KB |
Output is correct |
43 |
Correct |
19 ms |
23800 KB |
Output is correct |
44 |
Correct |
18 ms |
23800 KB |
Output is correct |
45 |
Correct |
18 ms |
23800 KB |
Output is correct |
46 |
Correct |
18 ms |
23800 KB |
Output is correct |
47 |
Correct |
19 ms |
23800 KB |
Output is correct |
48 |
Correct |
19 ms |
23800 KB |
Output is correct |
49 |
Correct |
18 ms |
23800 KB |
Output is correct |
50 |
Correct |
18 ms |
23800 KB |
Output is correct |
51 |
Correct |
18 ms |
23800 KB |
Output is correct |
52 |
Correct |
18 ms |
23800 KB |
Output is correct |
53 |
Correct |
19 ms |
23800 KB |
Output is correct |
54 |
Correct |
18 ms |
23800 KB |
Output is correct |
55 |
Correct |
19 ms |
23800 KB |
Output is correct |
56 |
Correct |
18 ms |
23800 KB |
Output is correct |
57 |
Correct |
18 ms |
23800 KB |
Output is correct |
58 |
Correct |
17 ms |
23800 KB |
Output is correct |
59 |
Correct |
18 ms |
23800 KB |
Output is correct |
60 |
Correct |
18 ms |
23800 KB |
Output is correct |
61 |
Correct |
18 ms |
23800 KB |
Output is correct |
62 |
Correct |
18 ms |
23800 KB |
Output is correct |
63 |
Correct |
18 ms |
23848 KB |
Output is correct |
64 |
Correct |
18 ms |
23800 KB |
Output is correct |
65 |
Correct |
138 ms |
33588 KB |
Output is correct |
66 |
Correct |
138 ms |
33976 KB |
Output is correct |
67 |
Correct |
141 ms |
35500 KB |
Output is correct |
68 |
Correct |
140 ms |
35504 KB |
Output is correct |
69 |
Correct |
139 ms |
35516 KB |
Output is correct |
70 |
Correct |
139 ms |
35508 KB |
Output is correct |
71 |
Correct |
140 ms |
35512 KB |
Output is correct |
72 |
Correct |
140 ms |
35512 KB |
Output is correct |
73 |
Correct |
140 ms |
35512 KB |
Output is correct |
74 |
Correct |
141 ms |
35508 KB |
Output is correct |
75 |
Correct |
138 ms |
35504 KB |
Output is correct |
76 |
Correct |
139 ms |
35504 KB |
Output is correct |
77 |
Correct |
139 ms |
35636 KB |
Output is correct |
78 |
Correct |
140 ms |
35512 KB |
Output is correct |
79 |
Correct |
139 ms |
35508 KB |
Output is correct |
80 |
Correct |
140 ms |
35508 KB |
Output is correct |
81 |
Correct |
140 ms |
35632 KB |
Output is correct |
82 |
Correct |
140 ms |
35508 KB |
Output is correct |
83 |
Correct |
140 ms |
35508 KB |
Output is correct |
84 |
Correct |
140 ms |
35508 KB |
Output is correct |
85 |
Correct |
142 ms |
35504 KB |
Output is correct |
86 |
Correct |
141 ms |
35504 KB |
Output is correct |
87 |
Correct |
140 ms |
35508 KB |
Output is correct |
88 |
Correct |
140 ms |
35504 KB |
Output is correct |
89 |
Correct |
139 ms |
35504 KB |
Output is correct |
90 |
Correct |
154 ms |
35508 KB |
Output is correct |
91 |
Correct |
140 ms |
35512 KB |
Output is correct |
92 |
Correct |
139 ms |
35508 KB |
Output is correct |
93 |
Correct |
140 ms |
35508 KB |
Output is correct |
94 |
Correct |
140 ms |
35508 KB |
Output is correct |
95 |
Correct |
140 ms |
35508 KB |
Output is correct |
96 |
Correct |
141 ms |
35508 KB |
Output is correct |
97 |
Correct |
139 ms |
35512 KB |
Output is correct |
98 |
Correct |
139 ms |
35508 KB |
Output is correct |
99 |
Correct |
139 ms |
35508 KB |
Output is correct |
100 |
Correct |
139 ms |
35508 KB |
Output is correct |
101 |
Correct |
139 ms |
35504 KB |
Output is correct |
102 |
Correct |
140 ms |
35504 KB |
Output is correct |