#include "bits/stdc++.h"
#define int long long
using namespace std;
mt19937_64 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
return uniform_int_distribution<int>(x, y)(rng);
}
const int MAXN = 2e3 + 10;
string a, b;
int n;
int ps[MAXN];
int dp[MAXN][MAXN][2];
int cnt0(int l, int r) {
int nums = ps[r] - ps[l];
int zero, one;
if(nums % 2 == 1) {
zero = one = (nums + 1) / 2;
}
else {
zero = nums / 2 + 1, one = nums / 2;
if(b[l] == '1') swap(zero, one);
}
return zero;
}
int cnt1(int l, int r) {
int nums = ps[r] - ps[l];
int zero, one;
if(nums % 2 == 1) {
zero = one = (nums + 1) / 2;
}
else {
zero = nums / 2 + 1, one = nums / 2;
if(b[l] == '1') swap(zero, one);
}
return one;
}
int mincnt(int l, int r) {
return min(cnt0(l, r), cnt1(l, r));
}
int dpf(int l, int r, int tog) {
while(l <= r && (a[l] ^ tog) == b[l]) l++;
while(l <= r && (a[r] ^ tog) == b[r]) r--;
if(l > r) return 0;
if(dp[l][r][tog] != -1) return dp[l][r][tog];
//cout << l << " " << r << " " << cnt0 << " " << cnt1 << "\n";
// set, then bruhopen
int ans = 1 + mincnt(l, r);
ans = min(ans, 1 + dpf(l, r, tog ^ 1));
for(int i=l; i<r; i++) {
ans = min(ans, 1 + mincnt(l, i) + dpf(i + 1, r, tog));
}
for(int i=l+1; i<=r; i++) {
ans = min(ans, 1 + mincnt(i, r) + dpf(l, i - 1, tog));
}
return ans;
}
void solve(int tc) {
cin >> n;
for(int i=1; i<=n; i++) {
for(int j=i; j<=n; j++) {
for(int k=0; k<2; k++) {
dp[i][j][k] = -1;
}
}
}
cin >> a >> b;
a = " " + a;
b = " " + b;
for(int i=2; i<=n; i++) ps[i] = ps[i-1] + (b[i] != b[i-1]);
cout << dpf(1, n, 0) << '\n';
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
g++ T2421.cpp -std=c++17 -O2 -o T2421
./T2421 < input.txt
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
15 ms |
2392 KB |
Output is correct |
9 |
Correct |
15 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2540 KB |
Output is correct |
12 |
Correct |
33 ms |
2648 KB |
Output is correct |
13 |
Correct |
12 ms |
2396 KB |
Output is correct |
14 |
Correct |
15 ms |
2536 KB |
Output is correct |
15 |
Correct |
2 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Incorrect |
80 ms |
2520 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
15 ms |
2392 KB |
Output is correct |
9 |
Correct |
15 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2540 KB |
Output is correct |
12 |
Correct |
33 ms |
2648 KB |
Output is correct |
13 |
Correct |
12 ms |
2396 KB |
Output is correct |
14 |
Correct |
15 ms |
2536 KB |
Output is correct |
15 |
Correct |
2 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Incorrect |
80 ms |
2520 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
1036 ms |
48512 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
15 ms |
2392 KB |
Output is correct |
9 |
Correct |
15 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2540 KB |
Output is correct |
12 |
Correct |
33 ms |
2648 KB |
Output is correct |
13 |
Correct |
12 ms |
2396 KB |
Output is correct |
14 |
Correct |
15 ms |
2536 KB |
Output is correct |
15 |
Correct |
2 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2516 KB |
Output is correct |
26 |
Incorrect |
80 ms |
2520 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |