#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;
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 |
19 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 |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
18 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 |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
18 ms |
23800 KB |
Output is correct |
15 |
Correct |
19 ms |
23800 KB |
Output is correct |
16 |
Correct |
18 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 |
23800 KB |
Output is correct |
26 |
Correct |
18 ms |
23800 KB |
Output is correct |
27 |
Correct |
17 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23804 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
17 ms |
23800 KB |
Output is correct |
31 |
Correct |
19 ms |
23800 KB |
Output is correct |
32 |
Correct |
17 ms |
23800 KB |
Output is correct |
33 |
Correct |
19 ms |
23800 KB |
Output is correct |
34 |
Correct |
17 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 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 |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
18 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 |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
18 ms |
23800 KB |
Output is correct |
15 |
Correct |
19 ms |
23800 KB |
Output is correct |
16 |
Correct |
18 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 |
23800 KB |
Output is correct |
26 |
Correct |
18 ms |
23800 KB |
Output is correct |
27 |
Correct |
17 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23804 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
17 ms |
23800 KB |
Output is correct |
31 |
Correct |
19 ms |
23800 KB |
Output is correct |
32 |
Correct |
17 ms |
23800 KB |
Output is correct |
33 |
Correct |
19 ms |
23800 KB |
Output is correct |
34 |
Correct |
17 ms |
23800 KB |
Output is correct |
35 |
Correct |
20 ms |
23800 KB |
Output is correct |
36 |
Correct |
18 ms |
23800 KB |
Output is correct |
37 |
Correct |
19 ms |
23800 KB |
Output is correct |
38 |
Correct |
18 ms |
23800 KB |
Output is correct |
39 |
Correct |
19 ms |
23800 KB |
Output is correct |
40 |
Correct |
19 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 |
18 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 |
23804 KB |
Output is correct |
51 |
Correct |
19 ms |
23800 KB |
Output is correct |
52 |
Correct |
18 ms |
23800 KB |
Output is correct |
53 |
Correct |
18 ms |
23800 KB |
Output is correct |
54 |
Correct |
18 ms |
23780 KB |
Output is correct |
55 |
Correct |
18 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 |
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 |
17 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 |
Runtime error |
179 ms |
68280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 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 |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
18 ms |
23800 KB |
Output is correct |
9 |
Correct |
18 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 |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
18 ms |
23800 KB |
Output is correct |
15 |
Correct |
19 ms |
23800 KB |
Output is correct |
16 |
Correct |
18 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 |
23800 KB |
Output is correct |
26 |
Correct |
18 ms |
23800 KB |
Output is correct |
27 |
Correct |
17 ms |
23800 KB |
Output is correct |
28 |
Correct |
18 ms |
23804 KB |
Output is correct |
29 |
Correct |
18 ms |
23800 KB |
Output is correct |
30 |
Correct |
17 ms |
23800 KB |
Output is correct |
31 |
Correct |
19 ms |
23800 KB |
Output is correct |
32 |
Correct |
17 ms |
23800 KB |
Output is correct |
33 |
Correct |
19 ms |
23800 KB |
Output is correct |
34 |
Correct |
17 ms |
23800 KB |
Output is correct |
35 |
Correct |
20 ms |
23800 KB |
Output is correct |
36 |
Correct |
18 ms |
23800 KB |
Output is correct |
37 |
Correct |
19 ms |
23800 KB |
Output is correct |
38 |
Correct |
18 ms |
23800 KB |
Output is correct |
39 |
Correct |
19 ms |
23800 KB |
Output is correct |
40 |
Correct |
19 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 |
18 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 |
23804 KB |
Output is correct |
51 |
Correct |
19 ms |
23800 KB |
Output is correct |
52 |
Correct |
18 ms |
23800 KB |
Output is correct |
53 |
Correct |
18 ms |
23800 KB |
Output is correct |
54 |
Correct |
18 ms |
23780 KB |
Output is correct |
55 |
Correct |
18 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 |
18 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 |
17 ms |
23800 KB |
Output is correct |
63 |
Correct |
18 ms |
23800 KB |
Output is correct |
64 |
Correct |
18 ms |
23800 KB |
Output is correct |
65 |
Runtime error |
179 ms |
68280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
66 |
Halted |
0 ms |
0 KB |
- |