#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define DBGRUN(stmt) stmt
#define DBG1(x) cout << #x << ": " << (x) << endl
#define DBG2(x, y) cout << #x << ": " << (x) << ", " << #y << ": " << (y) << endl
#define DBG3(x, y, z) cout << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << endl
#define DBG4(w, x, y, z) cout << #w << ": " << (w) << ", " << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << endl
#define DBG_ARR(a, e) cout << #a << ": "; for (int i = 0; i < e; i++) cout << a[i] << ' '; cout << endl
#define DBG_ARR2D(a, s1, e1, s2, e2) cout << #a << ": " << endl; for (int i = s1; i < e1; i++) { for (int j = s2; j < e2; j++) cout << a[i][j] << ' '; cout << '\n'; }cout << endl
#define DBG_VEC(v) cout << #v << ": "; for (auto i : v) cout << i << ' '; cout << endl
#define DBG_HERE cout << "reached line: " << __LINE__ << endl
#else
#define DBGRUN(stmt)
#define DBG1(x)
#define DBG2(x, y)
#define DBG3(x, y, z)
#define DBG4(w, x, y, z)
#define DBG_ARR(a, e)
#define DBG_ARR2D(a, s1, e1, s2, e2)
#define DBG_VEC(v)
#define DBG_HERE
#endif
#define int long long
#define f first
#define s second
#define pii pair<int, int>
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORR(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define ALLV(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
template <typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << ", " << a.second << ")"; return out; }
template<typename ...Ts, size_t ...Is> ostream& println_tuple_impl(ostream& os, tuple<Ts...> tuple, index_sequence<Is...>) { static_assert(sizeof...(Is) == sizeof...(Ts), "Indices must have same number of elements as tuple types!"); static_assert(sizeof...(Ts) > 0, "Cannot insert empty tuple into stream."); auto last = sizeof...(Ts) - 1; os << "("; return ((os << get<Is>(tuple) << (Is != last ? ", " : ")\n")), ...); }
template<typename ...Ts> ostream& operator<<(ostream& os, const tuple<Ts...>& tuple) { return println_tuple_impl(os, tuple, index_sequence_for<Ts...>{}); }
int N, memo[1000010][2][2][2];
vector<bool> AA, BB;
string A, B;
int dp(int idx, bool toggleOpen, bool rangeSetOpen, bool toggleFirst) {
if (memo[idx][toggleOpen][rangeSetOpen][toggleFirst] != -1)
return memo[idx][toggleOpen][rangeSetOpen][toggleFirst];
if (idx == N) return 0;
int res = 1e18;
if (!toggleOpen && !rangeSetOpen) {
// don't do any operations
if (AA[idx] == BB[idx]) res = min(res, dp(idx + 1, false, false, false));
// do toggle
if (AA[idx] == !BB[idx]) res = min(res, dp(idx + 1, true, false, true) + 1);
// do range_set
res = min(res, dp(idx + 1, false, true, false) + 1);
// do toggle and range_set, but in different orders
res = min(res, dp(idx + 1, true, true, true) + 2);
res = min(res, dp(idx + 1, true, true, false) + 2);
}
if (!toggleOpen && rangeSetOpen) {
// close rangeSetOpen
if (AA[idx] == BB[idx]) res = min(res, dp(idx + 1, false, false, false));
// do toggle & close rangeSetOpen
if (AA[idx] == !BB[idx]) res = min(res, dp(idx + 1, true, false, true) + 1);
// do rangSetOpen as it is
if (BB[idx] == BB[idx - 1]) res = min(res, dp(idx + 1, false, true, false));
// do toggle and range_set
if (!BB[idx - 1] == BB[idx]) res = min(res, dp(idx + 1, true, true, false) + 1);
}
if (toggleOpen && !rangeSetOpen) {
// close toggleOpen
if (AA[idx] == BB[idx]) res = min(res, dp(idx + 1, false, false, false));
// do rangeSetOpen & close toggle
res = min(res, dp(idx + 1, false, true, false) + 1);
// do toggle as it is
if (AA[idx] == !BB[idx]) res = min(res, dp(idx + 1, true, false, true));
// do toggle and range_set
res = min(res, dp(idx + 1, true, true, true) + 1);
}
if (toggleOpen && rangeSetOpen) {
// do it as it is
if (BB[idx - 1] == BB[idx]) res = min(res, dp(idx + 1, true, true, true));
// close rangeSetOpen
if (AA[idx] == !BB[idx]) res = min(res, dp(idx + 1, true, false, true));
// close toggle
if (BB[idx] == !BB[idx - 1]) res = min(res, dp(idx + 1, false, true, false));
// close both
if (AA[idx] == BB[idx]) res = min(res, dp(idx + 1, false, false, false));
}
memo[idx][toggleOpen][rangeSetOpen][toggleFirst] = res;
return res;
}
int32_t main() {
memset(memo, -1, sizeof memo);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N;
cin >> A >> B;
for (auto i : A) AA.push_back(i - '0');
for (auto i : B) BB.push_back(i - '0');
cout << dp(0, false, false, false);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
62932 KB |
Output is correct |
2 |
Correct |
24 ms |
62932 KB |
Output is correct |
3 |
Correct |
27 ms |
62808 KB |
Output is correct |
4 |
Correct |
24 ms |
62932 KB |
Output is correct |
5 |
Correct |
24 ms |
62928 KB |
Output is correct |
6 |
Correct |
27 ms |
62932 KB |
Output is correct |
7 |
Correct |
31 ms |
62924 KB |
Output is correct |
8 |
Correct |
24 ms |
62932 KB |
Output is correct |
9 |
Correct |
23 ms |
62868 KB |
Output is correct |
10 |
Correct |
24 ms |
62856 KB |
Output is correct |
11 |
Correct |
24 ms |
62836 KB |
Output is correct |
12 |
Correct |
25 ms |
62920 KB |
Output is correct |
13 |
Correct |
24 ms |
62936 KB |
Output is correct |
14 |
Correct |
26 ms |
62872 KB |
Output is correct |
15 |
Correct |
28 ms |
62816 KB |
Output is correct |
16 |
Correct |
26 ms |
62972 KB |
Output is correct |
17 |
Correct |
26 ms |
62932 KB |
Output is correct |
18 |
Correct |
25 ms |
62932 KB |
Output is correct |
19 |
Correct |
25 ms |
62920 KB |
Output is correct |
20 |
Correct |
24 ms |
62932 KB |
Output is correct |
21 |
Correct |
25 ms |
62932 KB |
Output is correct |
22 |
Correct |
25 ms |
62816 KB |
Output is correct |
23 |
Correct |
26 ms |
62820 KB |
Output is correct |
24 |
Correct |
24 ms |
62916 KB |
Output is correct |
25 |
Correct |
25 ms |
62920 KB |
Output is correct |
26 |
Correct |
24 ms |
62896 KB |
Output is correct |
27 |
Correct |
24 ms |
62940 KB |
Output is correct |
28 |
Correct |
28 ms |
62880 KB |
Output is correct |
29 |
Correct |
27 ms |
62828 KB |
Output is correct |
30 |
Correct |
25 ms |
62888 KB |
Output is correct |
31 |
Correct |
25 ms |
62888 KB |
Output is correct |
32 |
Correct |
25 ms |
62872 KB |
Output is correct |
33 |
Correct |
24 ms |
62848 KB |
Output is correct |
34 |
Correct |
25 ms |
62924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
62932 KB |
Output is correct |
2 |
Correct |
24 ms |
62932 KB |
Output is correct |
3 |
Correct |
27 ms |
62808 KB |
Output is correct |
4 |
Correct |
24 ms |
62932 KB |
Output is correct |
5 |
Correct |
24 ms |
62928 KB |
Output is correct |
6 |
Correct |
27 ms |
62932 KB |
Output is correct |
7 |
Correct |
31 ms |
62924 KB |
Output is correct |
8 |
Correct |
24 ms |
62932 KB |
Output is correct |
9 |
Correct |
23 ms |
62868 KB |
Output is correct |
10 |
Correct |
24 ms |
62856 KB |
Output is correct |
11 |
Correct |
24 ms |
62836 KB |
Output is correct |
12 |
Correct |
25 ms |
62920 KB |
Output is correct |
13 |
Correct |
24 ms |
62936 KB |
Output is correct |
14 |
Correct |
26 ms |
62872 KB |
Output is correct |
15 |
Correct |
28 ms |
62816 KB |
Output is correct |
16 |
Correct |
26 ms |
62972 KB |
Output is correct |
17 |
Correct |
26 ms |
62932 KB |
Output is correct |
18 |
Correct |
25 ms |
62932 KB |
Output is correct |
19 |
Correct |
25 ms |
62920 KB |
Output is correct |
20 |
Correct |
24 ms |
62932 KB |
Output is correct |
21 |
Correct |
25 ms |
62932 KB |
Output is correct |
22 |
Correct |
25 ms |
62816 KB |
Output is correct |
23 |
Correct |
26 ms |
62820 KB |
Output is correct |
24 |
Correct |
24 ms |
62916 KB |
Output is correct |
25 |
Correct |
25 ms |
62920 KB |
Output is correct |
26 |
Correct |
24 ms |
62896 KB |
Output is correct |
27 |
Correct |
24 ms |
62940 KB |
Output is correct |
28 |
Correct |
28 ms |
62880 KB |
Output is correct |
29 |
Correct |
27 ms |
62828 KB |
Output is correct |
30 |
Correct |
25 ms |
62888 KB |
Output is correct |
31 |
Correct |
25 ms |
62888 KB |
Output is correct |
32 |
Correct |
25 ms |
62872 KB |
Output is correct |
33 |
Correct |
24 ms |
62848 KB |
Output is correct |
34 |
Correct |
25 ms |
62924 KB |
Output is correct |
35 |
Correct |
26 ms |
63052 KB |
Output is correct |
36 |
Correct |
29 ms |
63060 KB |
Output is correct |
37 |
Correct |
27 ms |
63020 KB |
Output is correct |
38 |
Correct |
26 ms |
63060 KB |
Output is correct |
39 |
Correct |
27 ms |
63052 KB |
Output is correct |
40 |
Correct |
26 ms |
63132 KB |
Output is correct |
41 |
Correct |
28 ms |
63048 KB |
Output is correct |
42 |
Correct |
27 ms |
63068 KB |
Output is correct |
43 |
Correct |
27 ms |
63020 KB |
Output is correct |
44 |
Correct |
27 ms |
63064 KB |
Output is correct |
45 |
Correct |
26 ms |
63052 KB |
Output is correct |
46 |
Correct |
26 ms |
63056 KB |
Output is correct |
47 |
Correct |
27 ms |
63052 KB |
Output is correct |
48 |
Correct |
28 ms |
63076 KB |
Output is correct |
49 |
Correct |
26 ms |
63080 KB |
Output is correct |
50 |
Correct |
28 ms |
63052 KB |
Output is correct |
51 |
Correct |
26 ms |
63032 KB |
Output is correct |
52 |
Correct |
26 ms |
63060 KB |
Output is correct |
53 |
Correct |
27 ms |
63040 KB |
Output is correct |
54 |
Correct |
26 ms |
63012 KB |
Output is correct |
55 |
Correct |
26 ms |
62992 KB |
Output is correct |
56 |
Correct |
28 ms |
63064 KB |
Output is correct |
57 |
Correct |
24 ms |
63048 KB |
Output is correct |
58 |
Correct |
25 ms |
63092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
62932 KB |
Output is correct |
2 |
Correct |
25 ms |
62908 KB |
Output is correct |
3 |
Correct |
24 ms |
62940 KB |
Output is correct |
4 |
Correct |
25 ms |
62932 KB |
Output is correct |
5 |
Correct |
24 ms |
62932 KB |
Output is correct |
6 |
Correct |
28 ms |
62924 KB |
Output is correct |
7 |
Correct |
143 ms |
161132 KB |
Output is correct |
8 |
Correct |
145 ms |
161168 KB |
Output is correct |
9 |
Correct |
141 ms |
161168 KB |
Output is correct |
10 |
Correct |
139 ms |
161064 KB |
Output is correct |
11 |
Correct |
149 ms |
161048 KB |
Output is correct |
12 |
Correct |
129 ms |
161204 KB |
Output is correct |
13 |
Correct |
133 ms |
161140 KB |
Output is correct |
14 |
Correct |
141 ms |
161136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
62932 KB |
Output is correct |
2 |
Correct |
24 ms |
62932 KB |
Output is correct |
3 |
Correct |
27 ms |
62808 KB |
Output is correct |
4 |
Correct |
24 ms |
62932 KB |
Output is correct |
5 |
Correct |
24 ms |
62928 KB |
Output is correct |
6 |
Correct |
27 ms |
62932 KB |
Output is correct |
7 |
Correct |
31 ms |
62924 KB |
Output is correct |
8 |
Correct |
24 ms |
62932 KB |
Output is correct |
9 |
Correct |
23 ms |
62868 KB |
Output is correct |
10 |
Correct |
24 ms |
62856 KB |
Output is correct |
11 |
Correct |
24 ms |
62836 KB |
Output is correct |
12 |
Correct |
25 ms |
62920 KB |
Output is correct |
13 |
Correct |
24 ms |
62936 KB |
Output is correct |
14 |
Correct |
26 ms |
62872 KB |
Output is correct |
15 |
Correct |
28 ms |
62816 KB |
Output is correct |
16 |
Correct |
26 ms |
62972 KB |
Output is correct |
17 |
Correct |
26 ms |
62932 KB |
Output is correct |
18 |
Correct |
25 ms |
62932 KB |
Output is correct |
19 |
Correct |
25 ms |
62920 KB |
Output is correct |
20 |
Correct |
24 ms |
62932 KB |
Output is correct |
21 |
Correct |
25 ms |
62932 KB |
Output is correct |
22 |
Correct |
25 ms |
62816 KB |
Output is correct |
23 |
Correct |
26 ms |
62820 KB |
Output is correct |
24 |
Correct |
24 ms |
62916 KB |
Output is correct |
25 |
Correct |
25 ms |
62920 KB |
Output is correct |
26 |
Correct |
24 ms |
62896 KB |
Output is correct |
27 |
Correct |
24 ms |
62940 KB |
Output is correct |
28 |
Correct |
28 ms |
62880 KB |
Output is correct |
29 |
Correct |
27 ms |
62828 KB |
Output is correct |
30 |
Correct |
25 ms |
62888 KB |
Output is correct |
31 |
Correct |
25 ms |
62888 KB |
Output is correct |
32 |
Correct |
25 ms |
62872 KB |
Output is correct |
33 |
Correct |
24 ms |
62848 KB |
Output is correct |
34 |
Correct |
25 ms |
62924 KB |
Output is correct |
35 |
Correct |
26 ms |
63052 KB |
Output is correct |
36 |
Correct |
29 ms |
63060 KB |
Output is correct |
37 |
Correct |
27 ms |
63020 KB |
Output is correct |
38 |
Correct |
26 ms |
63060 KB |
Output is correct |
39 |
Correct |
27 ms |
63052 KB |
Output is correct |
40 |
Correct |
26 ms |
63132 KB |
Output is correct |
41 |
Correct |
28 ms |
63048 KB |
Output is correct |
42 |
Correct |
27 ms |
63068 KB |
Output is correct |
43 |
Correct |
27 ms |
63020 KB |
Output is correct |
44 |
Correct |
27 ms |
63064 KB |
Output is correct |
45 |
Correct |
26 ms |
63052 KB |
Output is correct |
46 |
Correct |
26 ms |
63056 KB |
Output is correct |
47 |
Correct |
27 ms |
63052 KB |
Output is correct |
48 |
Correct |
28 ms |
63076 KB |
Output is correct |
49 |
Correct |
26 ms |
63080 KB |
Output is correct |
50 |
Correct |
28 ms |
63052 KB |
Output is correct |
51 |
Correct |
26 ms |
63032 KB |
Output is correct |
52 |
Correct |
26 ms |
63060 KB |
Output is correct |
53 |
Correct |
27 ms |
63040 KB |
Output is correct |
54 |
Correct |
26 ms |
63012 KB |
Output is correct |
55 |
Correct |
26 ms |
62992 KB |
Output is correct |
56 |
Correct |
28 ms |
63064 KB |
Output is correct |
57 |
Correct |
24 ms |
63048 KB |
Output is correct |
58 |
Correct |
25 ms |
63092 KB |
Output is correct |
59 |
Correct |
24 ms |
62932 KB |
Output is correct |
60 |
Correct |
25 ms |
62908 KB |
Output is correct |
61 |
Correct |
24 ms |
62940 KB |
Output is correct |
62 |
Correct |
25 ms |
62932 KB |
Output is correct |
63 |
Correct |
24 ms |
62932 KB |
Output is correct |
64 |
Correct |
28 ms |
62924 KB |
Output is correct |
65 |
Correct |
143 ms |
161132 KB |
Output is correct |
66 |
Correct |
145 ms |
161168 KB |
Output is correct |
67 |
Correct |
141 ms |
161168 KB |
Output is correct |
68 |
Correct |
139 ms |
161064 KB |
Output is correct |
69 |
Correct |
149 ms |
161048 KB |
Output is correct |
70 |
Correct |
129 ms |
161204 KB |
Output is correct |
71 |
Correct |
133 ms |
161140 KB |
Output is correct |
72 |
Correct |
141 ms |
161136 KB |
Output is correct |
73 |
Correct |
137 ms |
161120 KB |
Output is correct |
74 |
Correct |
143 ms |
161124 KB |
Output is correct |
75 |
Correct |
143 ms |
161136 KB |
Output is correct |
76 |
Correct |
141 ms |
161068 KB |
Output is correct |
77 |
Correct |
145 ms |
161176 KB |
Output is correct |
78 |
Correct |
143 ms |
161080 KB |
Output is correct |
79 |
Correct |
140 ms |
161048 KB |
Output is correct |
80 |
Correct |
139 ms |
161164 KB |
Output is correct |
81 |
Correct |
145 ms |
161128 KB |
Output is correct |
82 |
Correct |
145 ms |
161164 KB |
Output is correct |
83 |
Correct |
138 ms |
161140 KB |
Output is correct |
84 |
Correct |
142 ms |
161236 KB |
Output is correct |
85 |
Correct |
145 ms |
161076 KB |
Output is correct |
86 |
Correct |
139 ms |
161048 KB |
Output is correct |
87 |
Correct |
142 ms |
161124 KB |
Output is correct |
88 |
Correct |
143 ms |
161156 KB |
Output is correct |
89 |
Correct |
140 ms |
161172 KB |
Output is correct |
90 |
Correct |
141 ms |
161140 KB |
Output is correct |
91 |
Correct |
142 ms |
161168 KB |
Output is correct |
92 |
Correct |
151 ms |
161228 KB |
Output is correct |
93 |
Correct |
153 ms |
161108 KB |
Output is correct |
94 |
Correct |
152 ms |
161164 KB |
Output is correct |
95 |
Correct |
149 ms |
161136 KB |
Output is correct |
96 |
Correct |
156 ms |
161240 KB |
Output is correct |
97 |
Correct |
152 ms |
161172 KB |
Output is correct |
98 |
Correct |
154 ms |
161060 KB |
Output is correct |
99 |
Correct |
154 ms |
161148 KB |
Output is correct |
100 |
Correct |
152 ms |
161040 KB |
Output is correct |
101 |
Correct |
154 ms |
161052 KB |
Output is correct |
102 |
Correct |
154 ms |
161164 KB |
Output is correct |