#include <iostream>
#include <vector>
using namespace std;
/*
States:
0 nothing
1 on
2 off
3 toggle
4 on + off
5 on + toggle
6 off + on
7 off + toggle
8 toggle + on
9 toggle + off
We consider the sequence of operations applied to a given position in order.
(*) No two toggle operations can be adjacent.
(*) No operation can be completely contained inside another on/off operation.
*/
const int off = 0;
const int on = 1;
const int toggle = 2;
string s[3] = {"OFF", "ON", "TOGGLE"};
int op_res(int a, int b)
{
if(b == off) return 0;
else if(b == on) return 1;
else return !a;
}
vector<int> ops[10];
int ops_res(int a, int b)
{
for(int x: ops[b]) a = op_res(a, x);
return a;
}
int cost_func(int ops1, int ops2)
{
int ans = ops[ops2].size();
int reducevalue = 0;
if(ops[ops1] == ops[ops2]) reducevalue = ops[ops2].size();
else
{
for(int x: ops[ops1])
for(int y: ops[ops2])
if(x == y)
reducevalue = 1;
}
return ans - reducevalue;
}
const long long INF = 1'000'000'000LL;
long long* dp1;
long long* dp2;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ops[0] = vector<int>{};
ops[1] = vector<int>{off};
ops[2] = vector<int>{on};
ops[3] = vector<int>{toggle};
ops[4] = vector<int>{off, on};
ops[5] = vector<int>{off, toggle};
ops[6] = vector<int>{on, off};
ops[7] = vector<int>{on, toggle};
ops[8] = vector<int>{toggle, off};
ops[9] = vector<int>{toggle, on};
// for(int i = 0; i <= 9; i++)
// for(int j = 0; j <= 9; j++)
// {
// for(int x: ops[i]) cerr << s[x] << ' ';
// cerr << "-> ";
// for(int y: ops[j]) cerr << s[y] << ' ';
// cerr << ": ";
// cerr << cost(i, j) << '\n';
// }
int N;
cin >> N;
string A;
cin >> A;
A = " " + A;
string B;
cin >> B;
B = " " + B;
dp1 = new long long[10];
dp2 = new long long[10];
dp2[0] = 0;
for(int o = 1; o < 10; o++) dp2[o] = INF;
long long cost[10][10];
for(int o1 = 0; o1 < 10; o1++)
for(int o2 = 0; o2 < 10; o2++)
cost[o1][o2] = cost_func(o1, o2);
for(int i = 1; i <= N; i++)
{
swap(dp1, dp2);
for(int o2 = 0; o2 < 10; o2++)
{
if(ops_res(A[i] - '0', o2) != (B[i] - '0'))
{
dp2[o2] = INF;
}
else
{
dp2[o2] = INF;
for(int o1 = 0; o1 < 10; o1++)
{
dp2[o2] = min(dp2[o2], dp1[o1] + cost[o1][o2]);
}
}
}
}
long long ans = INF;
for(int o = 0; o < 10; o++)
ans = min(ans, dp2[o]);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
312 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
1 ms |
204 KB |
Output is correct |
55 |
Correct |
1 ms |
204 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
92 ms |
6164 KB |
Output is correct |
8 |
Correct |
94 ms |
6168 KB |
Output is correct |
9 |
Correct |
94 ms |
6168 KB |
Output is correct |
10 |
Correct |
93 ms |
6172 KB |
Output is correct |
11 |
Correct |
93 ms |
6172 KB |
Output is correct |
12 |
Correct |
88 ms |
6164 KB |
Output is correct |
13 |
Correct |
101 ms |
6164 KB |
Output is correct |
14 |
Correct |
92 ms |
6176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
312 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
1 ms |
204 KB |
Output is correct |
55 |
Correct |
1 ms |
204 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
204 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
308 KB |
Output is correct |
65 |
Correct |
92 ms |
6164 KB |
Output is correct |
66 |
Correct |
94 ms |
6168 KB |
Output is correct |
67 |
Correct |
94 ms |
6168 KB |
Output is correct |
68 |
Correct |
93 ms |
6172 KB |
Output is correct |
69 |
Correct |
93 ms |
6172 KB |
Output is correct |
70 |
Correct |
88 ms |
6164 KB |
Output is correct |
71 |
Correct |
101 ms |
6164 KB |
Output is correct |
72 |
Correct |
92 ms |
6176 KB |
Output is correct |
73 |
Correct |
99 ms |
6164 KB |
Output is correct |
74 |
Correct |
99 ms |
6176 KB |
Output is correct |
75 |
Correct |
93 ms |
6284 KB |
Output is correct |
76 |
Correct |
93 ms |
6180 KB |
Output is correct |
77 |
Correct |
91 ms |
6164 KB |
Output is correct |
78 |
Correct |
91 ms |
6164 KB |
Output is correct |
79 |
Correct |
100 ms |
6164 KB |
Output is correct |
80 |
Correct |
96 ms |
6180 KB |
Output is correct |
81 |
Correct |
91 ms |
6172 KB |
Output is correct |
82 |
Correct |
92 ms |
6176 KB |
Output is correct |
83 |
Correct |
96 ms |
6164 KB |
Output is correct |
84 |
Correct |
96 ms |
6164 KB |
Output is correct |
85 |
Correct |
100 ms |
6180 KB |
Output is correct |
86 |
Correct |
97 ms |
6148 KB |
Output is correct |
87 |
Correct |
96 ms |
6168 KB |
Output is correct |
88 |
Correct |
96 ms |
6164 KB |
Output is correct |
89 |
Correct |
97 ms |
6164 KB |
Output is correct |
90 |
Correct |
104 ms |
6280 KB |
Output is correct |
91 |
Correct |
100 ms |
6172 KB |
Output is correct |
92 |
Correct |
103 ms |
6172 KB |
Output is correct |
93 |
Correct |
102 ms |
6164 KB |
Output is correct |
94 |
Correct |
104 ms |
6164 KB |
Output is correct |
95 |
Correct |
102 ms |
6180 KB |
Output is correct |
96 |
Correct |
108 ms |
6168 KB |
Output is correct |
97 |
Correct |
107 ms |
6188 KB |
Output is correct |
98 |
Correct |
103 ms |
6088 KB |
Output is correct |
99 |
Correct |
104 ms |
6280 KB |
Output is correct |
100 |
Correct |
103 ms |
6164 KB |
Output is correct |
101 |
Correct |
105 ms |
6096 KB |
Output is correct |
102 |
Correct |
102 ms |
6152 KB |
Output is correct |