#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e6 + 10;
ll dp[MAX_N][3][2];
string a, b;
int n;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> a >> b;
a = "x" + a;
b = "x" + b;
for(int i = 0; i < MAX_N; i ++) {
for(int j = 0; j < 3; j ++) {
for(int k = 0; k < 2; k ++) {
dp[i][j][k] = mod;
}
}
}
dp[0][2][0] = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 0; j < 2; j ++) {
for(int k = 0; k < 2; k ++) {
int curr = j;
if(k) {curr = (1 - curr);}
if(curr != b[i] - '0') {continue;}
for(int prevj = 0; prevj < 3; prevj ++) {
for(int prevk = 0; prevk < 2; prevk ++) {
int cost;
if(prevj != j) {
cost = 1;
} else {
cost = 0;
}
if(k == 1 && prevk != k) {
cost ++;
}
chkmin(dp[i][j][k], dp[i - 1][prevj][prevk] + cost);
}
}
}
}
if(a[i] == b[i]) {
for(int j = 0; j < 3; j ++) {
for(int k = 0; k < 2; k ++) {
chkmin(dp[i][2][0], dp[i - 1][j][k]);
}
}
} else {
for(int j = 0; j < 3; j ++) {
for(int k = 0; k < 2; k ++) {
chkmin(dp[i][2][1], dp[i - 1][j][k] + (1 - k));
}
}
}
}
ll ret = mod;
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 2; j ++) {
chkmin(ret, dp[n][i][j]);
}
}
cout << ret << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47180 KB |
Output is correct |
2 |
Correct |
22 ms |
47212 KB |
Output is correct |
3 |
Correct |
21 ms |
47208 KB |
Output is correct |
4 |
Correct |
21 ms |
47180 KB |
Output is correct |
5 |
Correct |
22 ms |
47180 KB |
Output is correct |
6 |
Correct |
21 ms |
47268 KB |
Output is correct |
7 |
Correct |
21 ms |
47268 KB |
Output is correct |
8 |
Correct |
21 ms |
47224 KB |
Output is correct |
9 |
Correct |
21 ms |
47176 KB |
Output is correct |
10 |
Correct |
21 ms |
47276 KB |
Output is correct |
11 |
Correct |
21 ms |
47240 KB |
Output is correct |
12 |
Correct |
21 ms |
47164 KB |
Output is correct |
13 |
Correct |
21 ms |
47180 KB |
Output is correct |
14 |
Correct |
22 ms |
47180 KB |
Output is correct |
15 |
Correct |
21 ms |
47180 KB |
Output is correct |
16 |
Correct |
21 ms |
47168 KB |
Output is correct |
17 |
Correct |
21 ms |
47268 KB |
Output is correct |
18 |
Correct |
21 ms |
47204 KB |
Output is correct |
19 |
Correct |
22 ms |
47172 KB |
Output is correct |
20 |
Correct |
22 ms |
47308 KB |
Output is correct |
21 |
Correct |
24 ms |
47256 KB |
Output is correct |
22 |
Correct |
21 ms |
47180 KB |
Output is correct |
23 |
Correct |
21 ms |
47156 KB |
Output is correct |
24 |
Correct |
21 ms |
47212 KB |
Output is correct |
25 |
Correct |
21 ms |
47172 KB |
Output is correct |
26 |
Correct |
22 ms |
47224 KB |
Output is correct |
27 |
Correct |
21 ms |
47180 KB |
Output is correct |
28 |
Correct |
22 ms |
47180 KB |
Output is correct |
29 |
Correct |
21 ms |
47164 KB |
Output is correct |
30 |
Correct |
21 ms |
47188 KB |
Output is correct |
31 |
Correct |
20 ms |
47180 KB |
Output is correct |
32 |
Correct |
21 ms |
47232 KB |
Output is correct |
33 |
Correct |
21 ms |
47164 KB |
Output is correct |
34 |
Correct |
21 ms |
47232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47180 KB |
Output is correct |
2 |
Correct |
22 ms |
47212 KB |
Output is correct |
3 |
Correct |
21 ms |
47208 KB |
Output is correct |
4 |
Correct |
21 ms |
47180 KB |
Output is correct |
5 |
Correct |
22 ms |
47180 KB |
Output is correct |
6 |
Correct |
21 ms |
47268 KB |
Output is correct |
7 |
Correct |
21 ms |
47268 KB |
Output is correct |
8 |
Correct |
21 ms |
47224 KB |
Output is correct |
9 |
Correct |
21 ms |
47176 KB |
Output is correct |
10 |
Correct |
21 ms |
47276 KB |
Output is correct |
11 |
Correct |
21 ms |
47240 KB |
Output is correct |
12 |
Correct |
21 ms |
47164 KB |
Output is correct |
13 |
Correct |
21 ms |
47180 KB |
Output is correct |
14 |
Correct |
22 ms |
47180 KB |
Output is correct |
15 |
Correct |
21 ms |
47180 KB |
Output is correct |
16 |
Correct |
21 ms |
47168 KB |
Output is correct |
17 |
Correct |
21 ms |
47268 KB |
Output is correct |
18 |
Correct |
21 ms |
47204 KB |
Output is correct |
19 |
Correct |
22 ms |
47172 KB |
Output is correct |
20 |
Correct |
22 ms |
47308 KB |
Output is correct |
21 |
Correct |
24 ms |
47256 KB |
Output is correct |
22 |
Correct |
21 ms |
47180 KB |
Output is correct |
23 |
Correct |
21 ms |
47156 KB |
Output is correct |
24 |
Correct |
21 ms |
47212 KB |
Output is correct |
25 |
Correct |
21 ms |
47172 KB |
Output is correct |
26 |
Correct |
22 ms |
47224 KB |
Output is correct |
27 |
Correct |
21 ms |
47180 KB |
Output is correct |
28 |
Correct |
22 ms |
47180 KB |
Output is correct |
29 |
Correct |
21 ms |
47164 KB |
Output is correct |
30 |
Correct |
21 ms |
47188 KB |
Output is correct |
31 |
Correct |
20 ms |
47180 KB |
Output is correct |
32 |
Correct |
21 ms |
47232 KB |
Output is correct |
33 |
Correct |
21 ms |
47164 KB |
Output is correct |
34 |
Correct |
21 ms |
47232 KB |
Output is correct |
35 |
Correct |
20 ms |
47176 KB |
Output is correct |
36 |
Correct |
21 ms |
47284 KB |
Output is correct |
37 |
Correct |
21 ms |
47196 KB |
Output is correct |
38 |
Correct |
22 ms |
47176 KB |
Output is correct |
39 |
Correct |
22 ms |
47248 KB |
Output is correct |
40 |
Correct |
23 ms |
47304 KB |
Output is correct |
41 |
Correct |
23 ms |
47180 KB |
Output is correct |
42 |
Correct |
20 ms |
47180 KB |
Output is correct |
43 |
Correct |
22 ms |
47236 KB |
Output is correct |
44 |
Correct |
23 ms |
47180 KB |
Output is correct |
45 |
Correct |
21 ms |
47228 KB |
Output is correct |
46 |
Correct |
21 ms |
47180 KB |
Output is correct |
47 |
Correct |
22 ms |
47256 KB |
Output is correct |
48 |
Correct |
21 ms |
47180 KB |
Output is correct |
49 |
Correct |
21 ms |
47180 KB |
Output is correct |
50 |
Correct |
21 ms |
47284 KB |
Output is correct |
51 |
Correct |
21 ms |
47244 KB |
Output is correct |
52 |
Correct |
21 ms |
47180 KB |
Output is correct |
53 |
Correct |
22 ms |
47228 KB |
Output is correct |
54 |
Correct |
23 ms |
47308 KB |
Output is correct |
55 |
Correct |
21 ms |
47268 KB |
Output is correct |
56 |
Correct |
22 ms |
47276 KB |
Output is correct |
57 |
Correct |
24 ms |
47180 KB |
Output is correct |
58 |
Correct |
24 ms |
47180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47256 KB |
Output is correct |
2 |
Correct |
21 ms |
47276 KB |
Output is correct |
3 |
Correct |
22 ms |
47184 KB |
Output is correct |
4 |
Correct |
21 ms |
47264 KB |
Output is correct |
5 |
Correct |
22 ms |
47200 KB |
Output is correct |
6 |
Correct |
21 ms |
47256 KB |
Output is correct |
7 |
Correct |
54 ms |
49148 KB |
Output is correct |
8 |
Correct |
54 ms |
51092 KB |
Output is correct |
9 |
Correct |
54 ms |
51160 KB |
Output is correct |
10 |
Correct |
54 ms |
51116 KB |
Output is correct |
11 |
Correct |
55 ms |
51104 KB |
Output is correct |
12 |
Correct |
54 ms |
51208 KB |
Output is correct |
13 |
Correct |
52 ms |
51124 KB |
Output is correct |
14 |
Correct |
53 ms |
51104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47180 KB |
Output is correct |
2 |
Correct |
22 ms |
47212 KB |
Output is correct |
3 |
Correct |
21 ms |
47208 KB |
Output is correct |
4 |
Correct |
21 ms |
47180 KB |
Output is correct |
5 |
Correct |
22 ms |
47180 KB |
Output is correct |
6 |
Correct |
21 ms |
47268 KB |
Output is correct |
7 |
Correct |
21 ms |
47268 KB |
Output is correct |
8 |
Correct |
21 ms |
47224 KB |
Output is correct |
9 |
Correct |
21 ms |
47176 KB |
Output is correct |
10 |
Correct |
21 ms |
47276 KB |
Output is correct |
11 |
Correct |
21 ms |
47240 KB |
Output is correct |
12 |
Correct |
21 ms |
47164 KB |
Output is correct |
13 |
Correct |
21 ms |
47180 KB |
Output is correct |
14 |
Correct |
22 ms |
47180 KB |
Output is correct |
15 |
Correct |
21 ms |
47180 KB |
Output is correct |
16 |
Correct |
21 ms |
47168 KB |
Output is correct |
17 |
Correct |
21 ms |
47268 KB |
Output is correct |
18 |
Correct |
21 ms |
47204 KB |
Output is correct |
19 |
Correct |
22 ms |
47172 KB |
Output is correct |
20 |
Correct |
22 ms |
47308 KB |
Output is correct |
21 |
Correct |
24 ms |
47256 KB |
Output is correct |
22 |
Correct |
21 ms |
47180 KB |
Output is correct |
23 |
Correct |
21 ms |
47156 KB |
Output is correct |
24 |
Correct |
21 ms |
47212 KB |
Output is correct |
25 |
Correct |
21 ms |
47172 KB |
Output is correct |
26 |
Correct |
22 ms |
47224 KB |
Output is correct |
27 |
Correct |
21 ms |
47180 KB |
Output is correct |
28 |
Correct |
22 ms |
47180 KB |
Output is correct |
29 |
Correct |
21 ms |
47164 KB |
Output is correct |
30 |
Correct |
21 ms |
47188 KB |
Output is correct |
31 |
Correct |
20 ms |
47180 KB |
Output is correct |
32 |
Correct |
21 ms |
47232 KB |
Output is correct |
33 |
Correct |
21 ms |
47164 KB |
Output is correct |
34 |
Correct |
21 ms |
47232 KB |
Output is correct |
35 |
Correct |
20 ms |
47176 KB |
Output is correct |
36 |
Correct |
21 ms |
47284 KB |
Output is correct |
37 |
Correct |
21 ms |
47196 KB |
Output is correct |
38 |
Correct |
22 ms |
47176 KB |
Output is correct |
39 |
Correct |
22 ms |
47248 KB |
Output is correct |
40 |
Correct |
23 ms |
47304 KB |
Output is correct |
41 |
Correct |
23 ms |
47180 KB |
Output is correct |
42 |
Correct |
20 ms |
47180 KB |
Output is correct |
43 |
Correct |
22 ms |
47236 KB |
Output is correct |
44 |
Correct |
23 ms |
47180 KB |
Output is correct |
45 |
Correct |
21 ms |
47228 KB |
Output is correct |
46 |
Correct |
21 ms |
47180 KB |
Output is correct |
47 |
Correct |
22 ms |
47256 KB |
Output is correct |
48 |
Correct |
21 ms |
47180 KB |
Output is correct |
49 |
Correct |
21 ms |
47180 KB |
Output is correct |
50 |
Correct |
21 ms |
47284 KB |
Output is correct |
51 |
Correct |
21 ms |
47244 KB |
Output is correct |
52 |
Correct |
21 ms |
47180 KB |
Output is correct |
53 |
Correct |
22 ms |
47228 KB |
Output is correct |
54 |
Correct |
23 ms |
47308 KB |
Output is correct |
55 |
Correct |
21 ms |
47268 KB |
Output is correct |
56 |
Correct |
22 ms |
47276 KB |
Output is correct |
57 |
Correct |
24 ms |
47180 KB |
Output is correct |
58 |
Correct |
24 ms |
47180 KB |
Output is correct |
59 |
Correct |
21 ms |
47256 KB |
Output is correct |
60 |
Correct |
21 ms |
47276 KB |
Output is correct |
61 |
Correct |
22 ms |
47184 KB |
Output is correct |
62 |
Correct |
21 ms |
47264 KB |
Output is correct |
63 |
Correct |
22 ms |
47200 KB |
Output is correct |
64 |
Correct |
21 ms |
47256 KB |
Output is correct |
65 |
Correct |
54 ms |
49148 KB |
Output is correct |
66 |
Correct |
54 ms |
51092 KB |
Output is correct |
67 |
Correct |
54 ms |
51160 KB |
Output is correct |
68 |
Correct |
54 ms |
51116 KB |
Output is correct |
69 |
Correct |
55 ms |
51104 KB |
Output is correct |
70 |
Correct |
54 ms |
51208 KB |
Output is correct |
71 |
Correct |
52 ms |
51124 KB |
Output is correct |
72 |
Correct |
53 ms |
51104 KB |
Output is correct |
73 |
Correct |
55 ms |
51132 KB |
Output is correct |
74 |
Correct |
55 ms |
51144 KB |
Output is correct |
75 |
Correct |
55 ms |
51104 KB |
Output is correct |
76 |
Correct |
53 ms |
51188 KB |
Output is correct |
77 |
Correct |
53 ms |
51076 KB |
Output is correct |
78 |
Correct |
56 ms |
51188 KB |
Output is correct |
79 |
Correct |
52 ms |
51192 KB |
Output is correct |
80 |
Correct |
51 ms |
51192 KB |
Output is correct |
81 |
Correct |
54 ms |
51140 KB |
Output is correct |
82 |
Correct |
53 ms |
51100 KB |
Output is correct |
83 |
Correct |
58 ms |
51128 KB |
Output is correct |
84 |
Correct |
58 ms |
51204 KB |
Output is correct |
85 |
Correct |
59 ms |
51148 KB |
Output is correct |
86 |
Correct |
73 ms |
51140 KB |
Output is correct |
87 |
Correct |
63 ms |
51096 KB |
Output is correct |
88 |
Correct |
57 ms |
51168 KB |
Output is correct |
89 |
Correct |
58 ms |
51140 KB |
Output is correct |
90 |
Correct |
59 ms |
51168 KB |
Output is correct |
91 |
Correct |
61 ms |
51172 KB |
Output is correct |
92 |
Correct |
65 ms |
51188 KB |
Output is correct |
93 |
Correct |
71 ms |
51144 KB |
Output is correct |
94 |
Correct |
68 ms |
51100 KB |
Output is correct |
95 |
Correct |
69 ms |
51148 KB |
Output is correct |
96 |
Correct |
65 ms |
51180 KB |
Output is correct |
97 |
Correct |
65 ms |
51176 KB |
Output is correct |
98 |
Correct |
65 ms |
51128 KB |
Output is correct |
99 |
Correct |
67 ms |
51192 KB |
Output is correct |
100 |
Correct |
66 ms |
51088 KB |
Output is correct |
101 |
Correct |
65 ms |
51144 KB |
Output is correct |
102 |
Correct |
65 ms |
51140 KB |
Output is correct |