답안 #373191

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373191 2021-03-03T16:52:49 Z luciocf Lamps (JOI19_lamps) C++14
100 / 100
973 ms 44460 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int maxn = 1e6+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 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 512 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 364 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 0 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 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 512 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 364 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 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 2 ms 492 KB Output is correct
37 Correct 1 ms 492 KB Output is correct
38 Correct 2 ms 492 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 2 ms 492 KB Output is correct
43 Correct 2 ms 492 KB Output is correct
44 Correct 3 ms 492 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 3 ms 492 KB Output is correct
47 Correct 2 ms 492 KB Output is correct
48 Correct 2 ms 492 KB Output is correct
49 Correct 2 ms 492 KB Output is correct
50 Correct 2 ms 492 KB Output is correct
51 Correct 2 ms 492 KB Output is correct
52 Correct 3 ms 492 KB Output is correct
53 Correct 2 ms 492 KB Output is correct
54 Correct 2 ms 492 KB Output is correct
55 Correct 2 ms 492 KB Output is correct
56 Correct 2 ms 492 KB Output is correct
57 Correct 2 ms 492 KB Output is correct
58 Correct 3 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 496 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 Correct 753 ms 44132 KB Output is correct
8 Correct 768 ms 44196 KB Output is correct
9 Correct 797 ms 44268 KB Output is correct
10 Correct 844 ms 44280 KB Output is correct
11 Correct 769 ms 44276 KB Output is correct
12 Correct 762 ms 44140 KB Output is correct
13 Correct 789 ms 44268 KB Output is correct
14 Correct 761 ms 44120 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 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 512 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 364 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 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 2 ms 492 KB Output is correct
37 Correct 1 ms 492 KB Output is correct
38 Correct 2 ms 492 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 2 ms 492 KB Output is correct
43 Correct 2 ms 492 KB Output is correct
44 Correct 3 ms 492 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 3 ms 492 KB Output is correct
47 Correct 2 ms 492 KB Output is correct
48 Correct 2 ms 492 KB Output is correct
49 Correct 2 ms 492 KB Output is correct
50 Correct 2 ms 492 KB Output is correct
51 Correct 2 ms 492 KB Output is correct
52 Correct 3 ms 492 KB Output is correct
53 Correct 2 ms 492 KB Output is correct
54 Correct 2 ms 492 KB Output is correct
55 Correct 2 ms 492 KB Output is correct
56 Correct 2 ms 492 KB Output is correct
57 Correct 2 ms 492 KB Output is correct
58 Correct 3 ms 492 KB Output is correct
59 Correct 1 ms 496 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 Correct 753 ms 44132 KB Output is correct
66 Correct 768 ms 44196 KB Output is correct
67 Correct 797 ms 44268 KB Output is correct
68 Correct 844 ms 44280 KB Output is correct
69 Correct 769 ms 44276 KB Output is correct
70 Correct 762 ms 44140 KB Output is correct
71 Correct 789 ms 44268 KB Output is correct
72 Correct 761 ms 44120 KB Output is correct
73 Correct 938 ms 44168 KB Output is correct
74 Correct 956 ms 44428 KB Output is correct
75 Correct 931 ms 44344 KB Output is correct
76 Correct 947 ms 44428 KB Output is correct
77 Correct 763 ms 44460 KB Output is correct
78 Correct 772 ms 44268 KB Output is correct
79 Correct 944 ms 44396 KB Output is correct
80 Correct 940 ms 44272 KB Output is correct
81 Correct 744 ms 44164 KB Output is correct
82 Correct 742 ms 44268 KB Output is correct
83 Correct 936 ms 44440 KB Output is correct
84 Correct 943 ms 44396 KB Output is correct
85 Correct 940 ms 44244 KB Output is correct
86 Correct 942 ms 44272 KB Output is correct
87 Correct 937 ms 44396 KB Output is correct
88 Correct 944 ms 44260 KB Output is correct
89 Correct 973 ms 44420 KB Output is correct
90 Correct 948 ms 44380 KB Output is correct
91 Correct 949 ms 44268 KB Output is correct
92 Correct 952 ms 44396 KB Output is correct
93 Correct 953 ms 44396 KB Output is correct
94 Correct 968 ms 44140 KB Output is correct
95 Correct 957 ms 44136 KB Output is correct
96 Correct 947 ms 44140 KB Output is correct
97 Correct 946 ms 44396 KB Output is correct
98 Correct 950 ms 44268 KB Output is correct
99 Correct 946 ms 44420 KB Output is correct
100 Correct 949 ms 44268 KB Output is correct
101 Correct 944 ms 44396 KB Output is correct
102 Correct 951 ms 44440 KB Output is correct