#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3+10;
const int inf = 1e9+10;
char a[maxn], b[maxn];
int ate[maxn][2];
int pref[maxn][2];
int dp[maxn][2];
struct SegmentTree
{
int tree[4*maxn];
void build(int node, int l, int r)
{
tree[node] = inf;
if (l == r) return;
int mid = (l+r)>>1;
build(2*node, l, mid); build(2*node+1, mid+1, r);
}
void upd(int node, int l, int r, int pos, int v)
{
if (l == r)
{
tree[node] = v;
return;
}
int mid = (l+r)>>1;
if (pos <= mid) upd(2*node, l, mid, pos, v);
else upd(2*node+1, mid+1, r, pos, v);
tree[node] = min(tree[2*node], tree[2*node+1]);
}
int query(int node, int l, int r, int a, int b)
{
if (l > r || l > b || r < a) return inf;
if (l >= a && r <= b) return tree[node];
int mid = (l+r)>>1;
return min(query(2*node, l, mid, a, b), query(2*node+1, mid+1, r, a, b));
}
} seg[2];
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf(" %c", &a[i]);
for (int i = 1; i <= n; i++)
scanf(" %c", &b[i]);
for (int i = 1; i <= n; i++)
{
if (a[i] == '1') ate[i][0] = 0;
else ate[i][0] = ate[i-1][0]+1;
if (a[i] == '0') ate[i][1] = 0;
else ate[i][1] = ate[i-1][1]+1;
}
seg[0].build(1, 0, n);
seg[1].build(1, 0, n);
seg[0].upd(1, 0, n, 0, b[1] == '1');
seg[1].upd(1, 0, n, 0, b[1] == '0');
for (int i = 1; i <= n; i++)
{
dp[i][0] = dp[i][1] = n+1;
pref[i][0] = pref[i-1][0];
if (i > 1) pref[i][0] += (b[i] == '1' && b[i-1] == '0');
pref[i][1] = pref[i-1][1];
if (i > 1) pref[i][1] += (b[i] == '0' && b[i-1] == '1');
if (a[i] == '0') dp[i][0] = pref[i][0] + seg[0].query(1, 0, n, i-ate[i][0], i-1);
if (a[i] == '1') dp[i][1] = pref[i][1] + seg[1].query(1, 0, n, i-ate[i][1], i-1);
dp[i][0] = min(dp[i][0], 1 + pref[i][0] + seg[0].query(1, 0, n, 0, i-ate[i][0]-1));
dp[i][1] = min(dp[i][1], 1 + pref[i][1] + seg[1].query(1, 0, n, 0, i-ate[i][1]-1));
seg[0].upd(1, 0, n, i, dp[i][1] - pref[i][0] + (b[i+1] == '1') - 2*(b[i] == '0' && b[i+1] == '1'));
seg[1].upd(1, 0, n, i, dp[i][0] - pref[i][1] + (b[i+1] == '0') - 2*(b[i] == '1' && b[i+1] == '0'));
}
printf("%d\n", min(dp[n][0], dp[n][1]));
}
Compilation message
lamp.cpp: In function 'int main()':
lamp.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
lamp.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf(" %c", &a[i]);
| ~~~~~^~~~~~~~~~~~~~
lamp.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf(" %c", &b[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
2 ms |
364 KB |
Output is correct |
37 |
Correct |
2 ms |
364 KB |
Output is correct |
38 |
Correct |
2 ms |
364 KB |
Output is correct |
39 |
Correct |
2 ms |
364 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
364 KB |
Output is correct |
43 |
Correct |
2 ms |
364 KB |
Output is correct |
44 |
Correct |
2 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
364 KB |
Output is correct |
46 |
Correct |
2 ms |
364 KB |
Output is correct |
47 |
Correct |
2 ms |
364 KB |
Output is correct |
48 |
Correct |
2 ms |
364 KB |
Output is correct |
49 |
Correct |
2 ms |
364 KB |
Output is correct |
50 |
Correct |
2 ms |
364 KB |
Output is correct |
51 |
Correct |
2 ms |
364 KB |
Output is correct |
52 |
Correct |
2 ms |
364 KB |
Output is correct |
53 |
Correct |
2 ms |
364 KB |
Output is correct |
54 |
Correct |
2 ms |
364 KB |
Output is correct |
55 |
Correct |
2 ms |
364 KB |
Output is correct |
56 |
Correct |
2 ms |
364 KB |
Output is correct |
57 |
Correct |
2 ms |
364 KB |
Output is correct |
58 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Execution timed out |
2 ms |
364 KB |
Time limit exceeded (wall clock) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
2 ms |
364 KB |
Output is correct |
37 |
Correct |
2 ms |
364 KB |
Output is correct |
38 |
Correct |
2 ms |
364 KB |
Output is correct |
39 |
Correct |
2 ms |
364 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
364 KB |
Output is correct |
43 |
Correct |
2 ms |
364 KB |
Output is correct |
44 |
Correct |
2 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
364 KB |
Output is correct |
46 |
Correct |
2 ms |
364 KB |
Output is correct |
47 |
Correct |
2 ms |
364 KB |
Output is correct |
48 |
Correct |
2 ms |
364 KB |
Output is correct |
49 |
Correct |
2 ms |
364 KB |
Output is correct |
50 |
Correct |
2 ms |
364 KB |
Output is correct |
51 |
Correct |
2 ms |
364 KB |
Output is correct |
52 |
Correct |
2 ms |
364 KB |
Output is correct |
53 |
Correct |
2 ms |
364 KB |
Output is correct |
54 |
Correct |
2 ms |
364 KB |
Output is correct |
55 |
Correct |
2 ms |
364 KB |
Output is correct |
56 |
Correct |
2 ms |
364 KB |
Output is correct |
57 |
Correct |
2 ms |
364 KB |
Output is correct |
58 |
Correct |
2 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Execution timed out |
2 ms |
364 KB |
Time limit exceeded (wall clock) |
66 |
Halted |
0 ms |
0 KB |
- |