#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e6 + 5, N = 1e5;
const ll oo = 1e18 + 1, base = 311;
const int lg = 15, M = 10;
const ll mod = 998244353, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n;
string a, b;
int dp[nmax][20 + 1][2];
int deg[nmax];
struct node{
int f[3];
}one[nmax];
int sz = 0, g[nmax], stu[nmax];
int F[2][100];
void add(vector<int> &v){
++sz;
g[sz] = v.size();
if(v.size() >= 1) one[sz].f[0] = v[0];
if(v.size() >= 2) one[sz].f[1] = v[1];
if(v.size() >= 3) one[sz].f[2] = v[2];
int res = 0;
for(int i = 0; i < 3; ++i){
res = res * 10 + one[sz].f[i];
}
stu[sz] = res;
deg[res] = sz;
for(int x = 0; x < 2; ++x){
int id = x;
for(int i = 0; i < 3; ++i){
if(one[sz].f[i] == 1) id = 0;
if(one[sz].f[i] == 2) id = 1;
if(one[sz].f[i] == 3) id ^= 1;
}
F[x][sz] = id;
}
}
vector<pii> adj[100], gg[100];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n;
cin >> a;
cin >> b;
a = ' ' + a;
b = ' ' + b;
memset(dp, 0x3f, sizeof dp);
dp[0][0][0] = 0;
vector<int> v;
for(int i = 1; i <= 3; ++i){
v.push_back(i);
do{
add(v);
}
while(next_permutation(v.begin(), v.end()));
v.clear();
}
for(int i = 1; i <= 3; ++i){
for(int j = i + 1; j <= 3; ++j){
v.push_back(i); v.push_back(j);
do{
add(v);
}
while(next_permutation(v.begin(), v.end()));
v.clear();
}
}
for(int i = 1; i <= 3; ++i) v.push_back(i);
do{
add(v);
}
while(next_permutation(v.begin(), v.end()));
v.clear();
for(int i = 0; i <= sz; ++i){
for(int j = 0; j <= sz; ++j){
if(g[i] + g[j] <= 3){
bool ok = 1;
for(int x = 0; x < 3; ++x){
if(!one[i].f[x]) break;
bool idx = 0;
for(int y = 0; y < 3; ++y){
if(!one[j].f[y])break;
if(one[i].f[x] == one[j].f[y]) idx = 1;
}
ok &= idx;
}
if(ok){
adj[i].push_back({j, g[j] - g[i]});
}
}
}
int res = 0;
gg[i].push_back({0, 0});
int p = -1;
for(int x = 0; x < 3; ++x){
if(!one[i].f[x]) break;
p = x + 1;
}
if(p != -1){
for(int msk = 1; msk < (1 << p); ++msk){
int res = 0,cnt =3;
for(int j = 0; j < p; ++j){
if(msk >> j & 1){
cnt--;
res = res * 10 + one[i].f[j];
}
}
while(cnt > 0){
res *= 10;
--cnt;
}
gg[i].push_back({deg[res], 0});
}
}
}
// for(int msk = 0; msk )
F[0][0] =0, F[1][0] = 1;
for(int i = 0; i < n;++i){
int x;
if(i == 0) x = 0;
else x = b[i] - '0';
for(int msk = 0; msk <= sz; ++msk){
for(auto [new_msk, cost] : gg[msk]){
dp[i][new_msk][x] = min(dp[i][new_msk][x], dp[i][msk][x] + cost);
}
}
for(int msk = sz; msk >= 0; --msk){
for(auto [new_msk, cost] : adj[msk]){
dp[i][new_msk][x] = min(dp[i][new_msk][x], dp[i][msk][x] + cost);
}
}
for(int msk = 0; msk <= sz; ++msk){
int y = a[i + 1] - '0';
int id = F[y][msk];
dp[i + 1][msk][id] = min(dp[i + 1][msk][id], dp[i][msk][x]);
}
}
int ans = 1e9;
for(int msk = 0; msk <= sz; ++msk) ans = min(ans, dp[n][msk][b[n]-'0']);
cout << ans;
}
/*
10
1111001110
1010100111
*/
Compilation message
lamp.cpp:47:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main(){
| ^~~~
lamp.cpp: In function 'int main()':
lamp.cpp:104:13: warning: unused variable 'res' [-Wunused-variable]
104 | int res = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
165500 KB |
Output is correct |
2 |
Correct |
40 ms |
165452 KB |
Output is correct |
3 |
Correct |
38 ms |
165564 KB |
Output is correct |
4 |
Correct |
43 ms |
165460 KB |
Output is correct |
5 |
Correct |
40 ms |
165464 KB |
Output is correct |
6 |
Correct |
40 ms |
165456 KB |
Output is correct |
7 |
Correct |
39 ms |
165460 KB |
Output is correct |
8 |
Correct |
41 ms |
165360 KB |
Output is correct |
9 |
Correct |
41 ms |
165456 KB |
Output is correct |
10 |
Correct |
44 ms |
165460 KB |
Output is correct |
11 |
Correct |
39 ms |
165460 KB |
Output is correct |
12 |
Correct |
41 ms |
165464 KB |
Output is correct |
13 |
Correct |
43 ms |
165456 KB |
Output is correct |
14 |
Correct |
43 ms |
165460 KB |
Output is correct |
15 |
Correct |
42 ms |
165436 KB |
Output is correct |
16 |
Correct |
40 ms |
165464 KB |
Output is correct |
17 |
Correct |
40 ms |
165560 KB |
Output is correct |
18 |
Correct |
41 ms |
165464 KB |
Output is correct |
19 |
Correct |
42 ms |
165436 KB |
Output is correct |
20 |
Correct |
45 ms |
165392 KB |
Output is correct |
21 |
Correct |
40 ms |
165456 KB |
Output is correct |
22 |
Correct |
38 ms |
165456 KB |
Output is correct |
23 |
Correct |
41 ms |
165464 KB |
Output is correct |
24 |
Correct |
41 ms |
165460 KB |
Output is correct |
25 |
Correct |
41 ms |
165456 KB |
Output is correct |
26 |
Correct |
41 ms |
165456 KB |
Output is correct |
27 |
Correct |
40 ms |
165460 KB |
Output is correct |
28 |
Correct |
42 ms |
165564 KB |
Output is correct |
29 |
Correct |
49 ms |
165456 KB |
Output is correct |
30 |
Correct |
40 ms |
165460 KB |
Output is correct |
31 |
Correct |
42 ms |
165460 KB |
Output is correct |
32 |
Correct |
43 ms |
165460 KB |
Output is correct |
33 |
Correct |
41 ms |
165456 KB |
Output is correct |
34 |
Correct |
46 ms |
165712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
165500 KB |
Output is correct |
2 |
Correct |
40 ms |
165452 KB |
Output is correct |
3 |
Correct |
38 ms |
165564 KB |
Output is correct |
4 |
Correct |
43 ms |
165460 KB |
Output is correct |
5 |
Correct |
40 ms |
165464 KB |
Output is correct |
6 |
Correct |
40 ms |
165456 KB |
Output is correct |
7 |
Correct |
39 ms |
165460 KB |
Output is correct |
8 |
Correct |
41 ms |
165360 KB |
Output is correct |
9 |
Correct |
41 ms |
165456 KB |
Output is correct |
10 |
Correct |
44 ms |
165460 KB |
Output is correct |
11 |
Correct |
39 ms |
165460 KB |
Output is correct |
12 |
Correct |
41 ms |
165464 KB |
Output is correct |
13 |
Correct |
43 ms |
165456 KB |
Output is correct |
14 |
Correct |
43 ms |
165460 KB |
Output is correct |
15 |
Correct |
42 ms |
165436 KB |
Output is correct |
16 |
Correct |
40 ms |
165464 KB |
Output is correct |
17 |
Correct |
40 ms |
165560 KB |
Output is correct |
18 |
Correct |
41 ms |
165464 KB |
Output is correct |
19 |
Correct |
42 ms |
165436 KB |
Output is correct |
20 |
Correct |
45 ms |
165392 KB |
Output is correct |
21 |
Correct |
40 ms |
165456 KB |
Output is correct |
22 |
Correct |
38 ms |
165456 KB |
Output is correct |
23 |
Correct |
41 ms |
165464 KB |
Output is correct |
24 |
Correct |
41 ms |
165460 KB |
Output is correct |
25 |
Correct |
41 ms |
165456 KB |
Output is correct |
26 |
Correct |
41 ms |
165456 KB |
Output is correct |
27 |
Correct |
40 ms |
165460 KB |
Output is correct |
28 |
Correct |
42 ms |
165564 KB |
Output is correct |
29 |
Correct |
49 ms |
165456 KB |
Output is correct |
30 |
Correct |
40 ms |
165460 KB |
Output is correct |
31 |
Correct |
42 ms |
165460 KB |
Output is correct |
32 |
Correct |
43 ms |
165460 KB |
Output is correct |
33 |
Correct |
41 ms |
165456 KB |
Output is correct |
34 |
Correct |
46 ms |
165712 KB |
Output is correct |
35 |
Correct |
44 ms |
165552 KB |
Output is correct |
36 |
Correct |
40 ms |
165452 KB |
Output is correct |
37 |
Correct |
38 ms |
165456 KB |
Output is correct |
38 |
Correct |
38 ms |
165392 KB |
Output is correct |
39 |
Correct |
42 ms |
165612 KB |
Output is correct |
40 |
Correct |
38 ms |
165468 KB |
Output is correct |
41 |
Correct |
38 ms |
165460 KB |
Output is correct |
42 |
Correct |
41 ms |
165460 KB |
Output is correct |
43 |
Correct |
40 ms |
165468 KB |
Output is correct |
44 |
Correct |
40 ms |
165712 KB |
Output is correct |
45 |
Correct |
39 ms |
165596 KB |
Output is correct |
46 |
Correct |
43 ms |
165492 KB |
Output is correct |
47 |
Correct |
47 ms |
165520 KB |
Output is correct |
48 |
Correct |
43 ms |
165456 KB |
Output is correct |
49 |
Correct |
43 ms |
165456 KB |
Output is correct |
50 |
Correct |
42 ms |
165460 KB |
Output is correct |
51 |
Correct |
42 ms |
165392 KB |
Output is correct |
52 |
Correct |
41 ms |
165460 KB |
Output is correct |
53 |
Correct |
43 ms |
165456 KB |
Output is correct |
54 |
Correct |
43 ms |
165516 KB |
Output is correct |
55 |
Correct |
41 ms |
165464 KB |
Output is correct |
56 |
Correct |
39 ms |
165480 KB |
Output is correct |
57 |
Correct |
40 ms |
165460 KB |
Output is correct |
58 |
Correct |
43 ms |
165456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
165404 KB |
Output is correct |
2 |
Correct |
42 ms |
165460 KB |
Output is correct |
3 |
Correct |
44 ms |
165464 KB |
Output is correct |
4 |
Correct |
40 ms |
165460 KB |
Output is correct |
5 |
Correct |
45 ms |
165448 KB |
Output is correct |
6 |
Correct |
46 ms |
165364 KB |
Output is correct |
7 |
Correct |
220 ms |
167416 KB |
Output is correct |
8 |
Correct |
226 ms |
167528 KB |
Output is correct |
9 |
Correct |
242 ms |
169164 KB |
Output is correct |
10 |
Correct |
228 ms |
169232 KB |
Output is correct |
11 |
Correct |
224 ms |
169208 KB |
Output is correct |
12 |
Correct |
266 ms |
169324 KB |
Output is correct |
13 |
Correct |
229 ms |
169216 KB |
Output is correct |
14 |
Correct |
240 ms |
169340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
165500 KB |
Output is correct |
2 |
Correct |
40 ms |
165452 KB |
Output is correct |
3 |
Correct |
38 ms |
165564 KB |
Output is correct |
4 |
Correct |
43 ms |
165460 KB |
Output is correct |
5 |
Correct |
40 ms |
165464 KB |
Output is correct |
6 |
Correct |
40 ms |
165456 KB |
Output is correct |
7 |
Correct |
39 ms |
165460 KB |
Output is correct |
8 |
Correct |
41 ms |
165360 KB |
Output is correct |
9 |
Correct |
41 ms |
165456 KB |
Output is correct |
10 |
Correct |
44 ms |
165460 KB |
Output is correct |
11 |
Correct |
39 ms |
165460 KB |
Output is correct |
12 |
Correct |
41 ms |
165464 KB |
Output is correct |
13 |
Correct |
43 ms |
165456 KB |
Output is correct |
14 |
Correct |
43 ms |
165460 KB |
Output is correct |
15 |
Correct |
42 ms |
165436 KB |
Output is correct |
16 |
Correct |
40 ms |
165464 KB |
Output is correct |
17 |
Correct |
40 ms |
165560 KB |
Output is correct |
18 |
Correct |
41 ms |
165464 KB |
Output is correct |
19 |
Correct |
42 ms |
165436 KB |
Output is correct |
20 |
Correct |
45 ms |
165392 KB |
Output is correct |
21 |
Correct |
40 ms |
165456 KB |
Output is correct |
22 |
Correct |
38 ms |
165456 KB |
Output is correct |
23 |
Correct |
41 ms |
165464 KB |
Output is correct |
24 |
Correct |
41 ms |
165460 KB |
Output is correct |
25 |
Correct |
41 ms |
165456 KB |
Output is correct |
26 |
Correct |
41 ms |
165456 KB |
Output is correct |
27 |
Correct |
40 ms |
165460 KB |
Output is correct |
28 |
Correct |
42 ms |
165564 KB |
Output is correct |
29 |
Correct |
49 ms |
165456 KB |
Output is correct |
30 |
Correct |
40 ms |
165460 KB |
Output is correct |
31 |
Correct |
42 ms |
165460 KB |
Output is correct |
32 |
Correct |
43 ms |
165460 KB |
Output is correct |
33 |
Correct |
41 ms |
165456 KB |
Output is correct |
34 |
Correct |
46 ms |
165712 KB |
Output is correct |
35 |
Correct |
44 ms |
165552 KB |
Output is correct |
36 |
Correct |
40 ms |
165452 KB |
Output is correct |
37 |
Correct |
38 ms |
165456 KB |
Output is correct |
38 |
Correct |
38 ms |
165392 KB |
Output is correct |
39 |
Correct |
42 ms |
165612 KB |
Output is correct |
40 |
Correct |
38 ms |
165468 KB |
Output is correct |
41 |
Correct |
38 ms |
165460 KB |
Output is correct |
42 |
Correct |
41 ms |
165460 KB |
Output is correct |
43 |
Correct |
40 ms |
165468 KB |
Output is correct |
44 |
Correct |
40 ms |
165712 KB |
Output is correct |
45 |
Correct |
39 ms |
165596 KB |
Output is correct |
46 |
Correct |
43 ms |
165492 KB |
Output is correct |
47 |
Correct |
47 ms |
165520 KB |
Output is correct |
48 |
Correct |
43 ms |
165456 KB |
Output is correct |
49 |
Correct |
43 ms |
165456 KB |
Output is correct |
50 |
Correct |
42 ms |
165460 KB |
Output is correct |
51 |
Correct |
42 ms |
165392 KB |
Output is correct |
52 |
Correct |
41 ms |
165460 KB |
Output is correct |
53 |
Correct |
43 ms |
165456 KB |
Output is correct |
54 |
Correct |
43 ms |
165516 KB |
Output is correct |
55 |
Correct |
41 ms |
165464 KB |
Output is correct |
56 |
Correct |
39 ms |
165480 KB |
Output is correct |
57 |
Correct |
40 ms |
165460 KB |
Output is correct |
58 |
Correct |
43 ms |
165456 KB |
Output is correct |
59 |
Correct |
42 ms |
165404 KB |
Output is correct |
60 |
Correct |
42 ms |
165460 KB |
Output is correct |
61 |
Correct |
44 ms |
165464 KB |
Output is correct |
62 |
Correct |
40 ms |
165460 KB |
Output is correct |
63 |
Correct |
45 ms |
165448 KB |
Output is correct |
64 |
Correct |
46 ms |
165364 KB |
Output is correct |
65 |
Correct |
220 ms |
167416 KB |
Output is correct |
66 |
Correct |
226 ms |
167528 KB |
Output is correct |
67 |
Correct |
242 ms |
169164 KB |
Output is correct |
68 |
Correct |
228 ms |
169232 KB |
Output is correct |
69 |
Correct |
224 ms |
169208 KB |
Output is correct |
70 |
Correct |
266 ms |
169324 KB |
Output is correct |
71 |
Correct |
229 ms |
169216 KB |
Output is correct |
72 |
Correct |
240 ms |
169340 KB |
Output is correct |
73 |
Correct |
234 ms |
169464 KB |
Output is correct |
74 |
Correct |
230 ms |
169400 KB |
Output is correct |
75 |
Correct |
262 ms |
169384 KB |
Output is correct |
76 |
Correct |
221 ms |
169460 KB |
Output is correct |
77 |
Correct |
225 ms |
169456 KB |
Output is correct |
78 |
Correct |
232 ms |
169468 KB |
Output is correct |
79 |
Correct |
227 ms |
169468 KB |
Output is correct |
80 |
Correct |
238 ms |
169464 KB |
Output is correct |
81 |
Correct |
224 ms |
169436 KB |
Output is correct |
82 |
Correct |
228 ms |
169316 KB |
Output is correct |
83 |
Correct |
225 ms |
169408 KB |
Output is correct |
84 |
Correct |
222 ms |
169400 KB |
Output is correct |
85 |
Correct |
255 ms |
169424 KB |
Output is correct |
86 |
Correct |
228 ms |
169456 KB |
Output is correct |
87 |
Correct |
236 ms |
169460 KB |
Output is correct |
88 |
Correct |
230 ms |
169456 KB |
Output is correct |
89 |
Correct |
236 ms |
169472 KB |
Output is correct |
90 |
Correct |
234 ms |
169456 KB |
Output is correct |
91 |
Correct |
237 ms |
169400 KB |
Output is correct |
92 |
Correct |
233 ms |
169376 KB |
Output is correct |
93 |
Correct |
223 ms |
169260 KB |
Output is correct |
94 |
Correct |
230 ms |
169540 KB |
Output is correct |
95 |
Correct |
223 ms |
169408 KB |
Output is correct |
96 |
Correct |
229 ms |
169456 KB |
Output is correct |
97 |
Correct |
231 ms |
169464 KB |
Output is correct |
98 |
Correct |
223 ms |
169484 KB |
Output is correct |
99 |
Correct |
248 ms |
169468 KB |
Output is correct |
100 |
Correct |
222 ms |
169452 KB |
Output is correct |
101 |
Correct |
217 ms |
169356 KB |
Output is correct |
102 |
Correct |
227 ms |
169324 KB |
Output is correct |