#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;
}
}
int val[nmax];
vector<int> v;
void update(int m){
v.clear();
for(int i = 1; i <= m; ++i) v.push_back(val[i]);
do{
add(v);
}
while(next_permutation(v.begin(), v.end()));
}
void calc(int i){
for(int j = val[i - 1] + 1; j <= 3; ++j){
val[i] = j;
update(i);
if(i < 3) calc(i + 1);
}
}
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;
calc(1);
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:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main(){
| ^~~~
lamp.cpp: In function 'int main()':
lamp.cpp:97:13: warning: unused variable 'res' [-Wunused-variable]
97 | int res = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
165200 KB |
Output is correct |
2 |
Correct |
37 ms |
165200 KB |
Output is correct |
3 |
Correct |
40 ms |
165312 KB |
Output is correct |
4 |
Correct |
36 ms |
165368 KB |
Output is correct |
5 |
Correct |
37 ms |
165200 KB |
Output is correct |
6 |
Correct |
40 ms |
165236 KB |
Output is correct |
7 |
Correct |
38 ms |
165204 KB |
Output is correct |
8 |
Correct |
39 ms |
165200 KB |
Output is correct |
9 |
Correct |
45 ms |
165200 KB |
Output is correct |
10 |
Correct |
41 ms |
165204 KB |
Output is correct |
11 |
Correct |
45 ms |
165204 KB |
Output is correct |
12 |
Correct |
39 ms |
165396 KB |
Output is correct |
13 |
Correct |
40 ms |
165204 KB |
Output is correct |
14 |
Correct |
39 ms |
165268 KB |
Output is correct |
15 |
Correct |
42 ms |
165216 KB |
Output is correct |
16 |
Correct |
40 ms |
165204 KB |
Output is correct |
17 |
Correct |
38 ms |
165200 KB |
Output is correct |
18 |
Correct |
38 ms |
165404 KB |
Output is correct |
19 |
Correct |
40 ms |
165212 KB |
Output is correct |
20 |
Correct |
37 ms |
165212 KB |
Output is correct |
21 |
Correct |
41 ms |
165200 KB |
Output is correct |
22 |
Correct |
37 ms |
165288 KB |
Output is correct |
23 |
Correct |
48 ms |
165200 KB |
Output is correct |
24 |
Correct |
42 ms |
165200 KB |
Output is correct |
25 |
Correct |
38 ms |
165204 KB |
Output is correct |
26 |
Correct |
39 ms |
165184 KB |
Output is correct |
27 |
Correct |
39 ms |
165204 KB |
Output is correct |
28 |
Correct |
40 ms |
165204 KB |
Output is correct |
29 |
Correct |
37 ms |
165200 KB |
Output is correct |
30 |
Correct |
38 ms |
165204 KB |
Output is correct |
31 |
Correct |
43 ms |
165204 KB |
Output is correct |
32 |
Correct |
36 ms |
165248 KB |
Output is correct |
33 |
Correct |
43 ms |
165208 KB |
Output is correct |
34 |
Correct |
42 ms |
165200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
165200 KB |
Output is correct |
2 |
Correct |
37 ms |
165200 KB |
Output is correct |
3 |
Correct |
40 ms |
165312 KB |
Output is correct |
4 |
Correct |
36 ms |
165368 KB |
Output is correct |
5 |
Correct |
37 ms |
165200 KB |
Output is correct |
6 |
Correct |
40 ms |
165236 KB |
Output is correct |
7 |
Correct |
38 ms |
165204 KB |
Output is correct |
8 |
Correct |
39 ms |
165200 KB |
Output is correct |
9 |
Correct |
45 ms |
165200 KB |
Output is correct |
10 |
Correct |
41 ms |
165204 KB |
Output is correct |
11 |
Correct |
45 ms |
165204 KB |
Output is correct |
12 |
Correct |
39 ms |
165396 KB |
Output is correct |
13 |
Correct |
40 ms |
165204 KB |
Output is correct |
14 |
Correct |
39 ms |
165268 KB |
Output is correct |
15 |
Correct |
42 ms |
165216 KB |
Output is correct |
16 |
Correct |
40 ms |
165204 KB |
Output is correct |
17 |
Correct |
38 ms |
165200 KB |
Output is correct |
18 |
Correct |
38 ms |
165404 KB |
Output is correct |
19 |
Correct |
40 ms |
165212 KB |
Output is correct |
20 |
Correct |
37 ms |
165212 KB |
Output is correct |
21 |
Correct |
41 ms |
165200 KB |
Output is correct |
22 |
Correct |
37 ms |
165288 KB |
Output is correct |
23 |
Correct |
48 ms |
165200 KB |
Output is correct |
24 |
Correct |
42 ms |
165200 KB |
Output is correct |
25 |
Correct |
38 ms |
165204 KB |
Output is correct |
26 |
Correct |
39 ms |
165184 KB |
Output is correct |
27 |
Correct |
39 ms |
165204 KB |
Output is correct |
28 |
Correct |
40 ms |
165204 KB |
Output is correct |
29 |
Correct |
37 ms |
165200 KB |
Output is correct |
30 |
Correct |
38 ms |
165204 KB |
Output is correct |
31 |
Correct |
43 ms |
165204 KB |
Output is correct |
32 |
Correct |
36 ms |
165248 KB |
Output is correct |
33 |
Correct |
43 ms |
165208 KB |
Output is correct |
34 |
Correct |
42 ms |
165200 KB |
Output is correct |
35 |
Correct |
37 ms |
165212 KB |
Output is correct |
36 |
Correct |
41 ms |
165280 KB |
Output is correct |
37 |
Correct |
46 ms |
165312 KB |
Output is correct |
38 |
Correct |
43 ms |
165204 KB |
Output is correct |
39 |
Correct |
43 ms |
165416 KB |
Output is correct |
40 |
Correct |
39 ms |
165212 KB |
Output is correct |
41 |
Correct |
38 ms |
165224 KB |
Output is correct |
42 |
Correct |
41 ms |
165224 KB |
Output is correct |
43 |
Correct |
42 ms |
165200 KB |
Output is correct |
44 |
Correct |
41 ms |
165252 KB |
Output is correct |
45 |
Correct |
40 ms |
165200 KB |
Output is correct |
46 |
Correct |
41 ms |
165200 KB |
Output is correct |
47 |
Correct |
38 ms |
165200 KB |
Output is correct |
48 |
Correct |
38 ms |
165332 KB |
Output is correct |
49 |
Correct |
39 ms |
165200 KB |
Output is correct |
50 |
Correct |
37 ms |
165200 KB |
Output is correct |
51 |
Correct |
43 ms |
165204 KB |
Output is correct |
52 |
Correct |
37 ms |
165268 KB |
Output is correct |
53 |
Correct |
40 ms |
165200 KB |
Output is correct |
54 |
Correct |
42 ms |
165200 KB |
Output is correct |
55 |
Correct |
48 ms |
165200 KB |
Output is correct |
56 |
Correct |
37 ms |
165204 KB |
Output is correct |
57 |
Correct |
48 ms |
165204 KB |
Output is correct |
58 |
Correct |
39 ms |
165388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
165200 KB |
Output is correct |
2 |
Correct |
44 ms |
165176 KB |
Output is correct |
3 |
Correct |
41 ms |
165204 KB |
Output is correct |
4 |
Correct |
45 ms |
165200 KB |
Output is correct |
5 |
Correct |
39 ms |
165212 KB |
Output is correct |
6 |
Correct |
47 ms |
165200 KB |
Output is correct |
7 |
Correct |
261 ms |
167264 KB |
Output is correct |
8 |
Correct |
234 ms |
167152 KB |
Output is correct |
9 |
Correct |
226 ms |
167168 KB |
Output is correct |
10 |
Correct |
254 ms |
167156 KB |
Output is correct |
11 |
Correct |
238 ms |
167188 KB |
Output is correct |
12 |
Correct |
237 ms |
167164 KB |
Output is correct |
13 |
Correct |
234 ms |
167152 KB |
Output is correct |
14 |
Correct |
246 ms |
167160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
165200 KB |
Output is correct |
2 |
Correct |
37 ms |
165200 KB |
Output is correct |
3 |
Correct |
40 ms |
165312 KB |
Output is correct |
4 |
Correct |
36 ms |
165368 KB |
Output is correct |
5 |
Correct |
37 ms |
165200 KB |
Output is correct |
6 |
Correct |
40 ms |
165236 KB |
Output is correct |
7 |
Correct |
38 ms |
165204 KB |
Output is correct |
8 |
Correct |
39 ms |
165200 KB |
Output is correct |
9 |
Correct |
45 ms |
165200 KB |
Output is correct |
10 |
Correct |
41 ms |
165204 KB |
Output is correct |
11 |
Correct |
45 ms |
165204 KB |
Output is correct |
12 |
Correct |
39 ms |
165396 KB |
Output is correct |
13 |
Correct |
40 ms |
165204 KB |
Output is correct |
14 |
Correct |
39 ms |
165268 KB |
Output is correct |
15 |
Correct |
42 ms |
165216 KB |
Output is correct |
16 |
Correct |
40 ms |
165204 KB |
Output is correct |
17 |
Correct |
38 ms |
165200 KB |
Output is correct |
18 |
Correct |
38 ms |
165404 KB |
Output is correct |
19 |
Correct |
40 ms |
165212 KB |
Output is correct |
20 |
Correct |
37 ms |
165212 KB |
Output is correct |
21 |
Correct |
41 ms |
165200 KB |
Output is correct |
22 |
Correct |
37 ms |
165288 KB |
Output is correct |
23 |
Correct |
48 ms |
165200 KB |
Output is correct |
24 |
Correct |
42 ms |
165200 KB |
Output is correct |
25 |
Correct |
38 ms |
165204 KB |
Output is correct |
26 |
Correct |
39 ms |
165184 KB |
Output is correct |
27 |
Correct |
39 ms |
165204 KB |
Output is correct |
28 |
Correct |
40 ms |
165204 KB |
Output is correct |
29 |
Correct |
37 ms |
165200 KB |
Output is correct |
30 |
Correct |
38 ms |
165204 KB |
Output is correct |
31 |
Correct |
43 ms |
165204 KB |
Output is correct |
32 |
Correct |
36 ms |
165248 KB |
Output is correct |
33 |
Correct |
43 ms |
165208 KB |
Output is correct |
34 |
Correct |
42 ms |
165200 KB |
Output is correct |
35 |
Correct |
37 ms |
165212 KB |
Output is correct |
36 |
Correct |
41 ms |
165280 KB |
Output is correct |
37 |
Correct |
46 ms |
165312 KB |
Output is correct |
38 |
Correct |
43 ms |
165204 KB |
Output is correct |
39 |
Correct |
43 ms |
165416 KB |
Output is correct |
40 |
Correct |
39 ms |
165212 KB |
Output is correct |
41 |
Correct |
38 ms |
165224 KB |
Output is correct |
42 |
Correct |
41 ms |
165224 KB |
Output is correct |
43 |
Correct |
42 ms |
165200 KB |
Output is correct |
44 |
Correct |
41 ms |
165252 KB |
Output is correct |
45 |
Correct |
40 ms |
165200 KB |
Output is correct |
46 |
Correct |
41 ms |
165200 KB |
Output is correct |
47 |
Correct |
38 ms |
165200 KB |
Output is correct |
48 |
Correct |
38 ms |
165332 KB |
Output is correct |
49 |
Correct |
39 ms |
165200 KB |
Output is correct |
50 |
Correct |
37 ms |
165200 KB |
Output is correct |
51 |
Correct |
43 ms |
165204 KB |
Output is correct |
52 |
Correct |
37 ms |
165268 KB |
Output is correct |
53 |
Correct |
40 ms |
165200 KB |
Output is correct |
54 |
Correct |
42 ms |
165200 KB |
Output is correct |
55 |
Correct |
48 ms |
165200 KB |
Output is correct |
56 |
Correct |
37 ms |
165204 KB |
Output is correct |
57 |
Correct |
48 ms |
165204 KB |
Output is correct |
58 |
Correct |
39 ms |
165388 KB |
Output is correct |
59 |
Correct |
39 ms |
165200 KB |
Output is correct |
60 |
Correct |
44 ms |
165176 KB |
Output is correct |
61 |
Correct |
41 ms |
165204 KB |
Output is correct |
62 |
Correct |
45 ms |
165200 KB |
Output is correct |
63 |
Correct |
39 ms |
165212 KB |
Output is correct |
64 |
Correct |
47 ms |
165200 KB |
Output is correct |
65 |
Correct |
261 ms |
167264 KB |
Output is correct |
66 |
Correct |
234 ms |
167152 KB |
Output is correct |
67 |
Correct |
226 ms |
167168 KB |
Output is correct |
68 |
Correct |
254 ms |
167156 KB |
Output is correct |
69 |
Correct |
238 ms |
167188 KB |
Output is correct |
70 |
Correct |
237 ms |
167164 KB |
Output is correct |
71 |
Correct |
234 ms |
167152 KB |
Output is correct |
72 |
Correct |
246 ms |
167160 KB |
Output is correct |
73 |
Correct |
231 ms |
167376 KB |
Output is correct |
74 |
Correct |
233 ms |
167160 KB |
Output is correct |
75 |
Correct |
241 ms |
167160 KB |
Output is correct |
76 |
Correct |
244 ms |
167152 KB |
Output is correct |
77 |
Correct |
232 ms |
167156 KB |
Output is correct |
78 |
Correct |
234 ms |
167168 KB |
Output is correct |
79 |
Correct |
224 ms |
167156 KB |
Output is correct |
80 |
Correct |
230 ms |
167164 KB |
Output is correct |
81 |
Correct |
268 ms |
167164 KB |
Output is correct |
82 |
Correct |
252 ms |
167276 KB |
Output is correct |
83 |
Correct |
228 ms |
167156 KB |
Output is correct |
84 |
Correct |
231 ms |
167152 KB |
Output is correct |
85 |
Correct |
238 ms |
167156 KB |
Output is correct |
86 |
Correct |
233 ms |
167156 KB |
Output is correct |
87 |
Correct |
229 ms |
167160 KB |
Output is correct |
88 |
Correct |
236 ms |
167172 KB |
Output is correct |
89 |
Correct |
236 ms |
167160 KB |
Output is correct |
90 |
Correct |
239 ms |
167272 KB |
Output is correct |
91 |
Correct |
255 ms |
167156 KB |
Output is correct |
92 |
Correct |
238 ms |
167192 KB |
Output is correct |
93 |
Correct |
245 ms |
167156 KB |
Output is correct |
94 |
Correct |
237 ms |
167184 KB |
Output is correct |
95 |
Correct |
232 ms |
167152 KB |
Output is correct |
96 |
Correct |
238 ms |
167160 KB |
Output is correct |
97 |
Correct |
226 ms |
167160 KB |
Output is correct |
98 |
Correct |
237 ms |
167156 KB |
Output is correct |
99 |
Correct |
233 ms |
167168 KB |
Output is correct |
100 |
Correct |
221 ms |
167172 KB |
Output is correct |
101 |
Correct |
221 ms |
167164 KB |
Output is correct |
102 |
Correct |
228 ms |
167164 KB |
Output is correct |