답안 #705312

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705312 2023-03-03T22:47:53 Z rainboy Rope (JOI17_rope) C
100 / 100
1118 ms 72776 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	1000000
#define M	N
#define N_	(1 << 20)	/* N_ = pow2(ceil(log2(M))) */

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

int *ei[M], eo[M];

void append(int a, int i) {
	int o = eo[a]++;

	if (o >= 2 && (o & o - 1) == 0)
		ei[a] = (int *) realloc(ei[a], o * 2 * sizeof *ei[a]);
	ei[a][o] = i;
}

int st[N_ * 2], n_;

void pul(int i) {
	st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}

void update(int i, int x) {
	st[i += n_] += x;
	while (i > 1)
		pul(i >>= 1);
}

int main() {
	static int aa[N], ans[M];
	int n, m, r, i, a, o, ka, kb;

	scanf("%d%d", &n, &m);
	if (m == 1) {
		printf("0\n");
		return 0;
	}
	for (a = 0; a < n; a++)
		ei[a] = (int *) malloc(2 * sizeof *ei[a]);
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]), aa[i]--;
		append(aa[i], i);
	}
	n_ = 1;
	while (n_ < m)
		n_ <<= 1;
	for (i = 0; i < n; i++)
		update(aa[i], 1);
	memset(ans, 0x3f, n * sizeof *ans);
	for (r = 0; r < 2; r++)
		for (a = 0; a < m; a++) {
			ka = st[n_ + a];
			for (o = eo[a]; o--; ) {
				i = ei[a][o];
				update(aa[i], -1);
				if (i % 2 == r) {
					if (i > 0)
						update(aa[i - 1], -1);
				} else {
					if (i + 1 < n)
						update(aa[i + 1], -1);
				}
			}
			kb = st[1];
			ans[a] = min(ans[a], n - ka - kb);
			for (o = eo[a]; o--; ) {
				i = ei[a][o];
				update(aa[i], 1);
				if (i % 2 == r) {
					if (i > 0)
						update(aa[i - 1], 1);
				} else {
					if (i + 1 < n)
						update(aa[i + 1], 1);
				}
			}
		}
	for (a = 0; a < m; a++)
		printf("%d\n", ans[a]);
	return 0;
}

Compilation message

rope.c: In function 'append':
rope.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
rope.c: In function 'main':
rope.c:38:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
rope.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 20 ms 5864 KB Output is correct
28 Correct 16 ms 5864 KB Output is correct
29 Correct 20 ms 5784 KB Output is correct
30 Correct 19 ms 5868 KB Output is correct
31 Correct 21 ms 5844 KB Output is correct
32 Correct 17 ms 5716 KB Output is correct
33 Correct 19 ms 5844 KB Output is correct
34 Correct 18 ms 5816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 20 ms 5864 KB Output is correct
28 Correct 16 ms 5864 KB Output is correct
29 Correct 20 ms 5784 KB Output is correct
30 Correct 19 ms 5868 KB Output is correct
31 Correct 21 ms 5844 KB Output is correct
32 Correct 17 ms 5716 KB Output is correct
33 Correct 19 ms 5844 KB Output is correct
34 Correct 18 ms 5816 KB Output is correct
35 Correct 31 ms 5944 KB Output is correct
36 Correct 30 ms 5956 KB Output is correct
37 Correct 31 ms 5844 KB Output is correct
38 Correct 31 ms 5844 KB Output is correct
39 Correct 32 ms 5960 KB Output is correct
40 Correct 29 ms 6016 KB Output is correct
41 Correct 29 ms 5972 KB Output is correct
42 Correct 30 ms 5948 KB Output is correct
43 Correct 28 ms 5980 KB Output is correct
44 Correct 29 ms 6028 KB Output is correct
45 Correct 29 ms 6040 KB Output is correct
46 Correct 28 ms 5972 KB Output is correct
47 Correct 30 ms 6048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 20 ms 5864 KB Output is correct
28 Correct 16 ms 5864 KB Output is correct
29 Correct 20 ms 5784 KB Output is correct
30 Correct 19 ms 5868 KB Output is correct
31 Correct 21 ms 5844 KB Output is correct
32 Correct 17 ms 5716 KB Output is correct
33 Correct 19 ms 5844 KB Output is correct
34 Correct 18 ms 5816 KB Output is correct
35 Correct 31 ms 5944 KB Output is correct
36 Correct 30 ms 5956 KB Output is correct
37 Correct 31 ms 5844 KB Output is correct
38 Correct 31 ms 5844 KB Output is correct
39 Correct 32 ms 5960 KB Output is correct
40 Correct 29 ms 6016 KB Output is correct
41 Correct 29 ms 5972 KB Output is correct
42 Correct 30 ms 5948 KB Output is correct
43 Correct 28 ms 5980 KB Output is correct
44 Correct 29 ms 6028 KB Output is correct
45 Correct 29 ms 6040 KB Output is correct
46 Correct 28 ms 5972 KB Output is correct
47 Correct 30 ms 6048 KB Output is correct
48 Correct 424 ms 58584 KB Output is correct
49 Correct 409 ms 58584 KB Output is correct
50 Correct 427 ms 58564 KB Output is correct
51 Correct 394 ms 58188 KB Output is correct
52 Correct 342 ms 57008 KB Output is correct
53 Correct 389 ms 57560 KB Output is correct
54 Correct 369 ms 56524 KB Output is correct
55 Correct 378 ms 56452 KB Output is correct
56 Correct 359 ms 56140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 20 ms 5864 KB Output is correct
28 Correct 16 ms 5864 KB Output is correct
29 Correct 20 ms 5784 KB Output is correct
30 Correct 19 ms 5868 KB Output is correct
31 Correct 21 ms 5844 KB Output is correct
32 Correct 17 ms 5716 KB Output is correct
33 Correct 19 ms 5844 KB Output is correct
34 Correct 18 ms 5816 KB Output is correct
35 Correct 31 ms 5944 KB Output is correct
36 Correct 30 ms 5956 KB Output is correct
37 Correct 31 ms 5844 KB Output is correct
38 Correct 31 ms 5844 KB Output is correct
39 Correct 32 ms 5960 KB Output is correct
40 Correct 29 ms 6016 KB Output is correct
41 Correct 29 ms 5972 KB Output is correct
42 Correct 30 ms 5948 KB Output is correct
43 Correct 28 ms 5980 KB Output is correct
44 Correct 29 ms 6028 KB Output is correct
45 Correct 29 ms 6040 KB Output is correct
46 Correct 28 ms 5972 KB Output is correct
47 Correct 30 ms 6048 KB Output is correct
48 Correct 424 ms 58584 KB Output is correct
49 Correct 409 ms 58584 KB Output is correct
50 Correct 427 ms 58564 KB Output is correct
51 Correct 394 ms 58188 KB Output is correct
52 Correct 342 ms 57008 KB Output is correct
53 Correct 389 ms 57560 KB Output is correct
54 Correct 369 ms 56524 KB Output is correct
55 Correct 378 ms 56452 KB Output is correct
56 Correct 359 ms 56140 KB Output is correct
57 Correct 1118 ms 72776 KB Output is correct
58 Correct 983 ms 65352 KB Output is correct
59 Correct 994 ms 65336 KB Output is correct
60 Correct 1020 ms 67128 KB Output is correct
61 Correct 1026 ms 67116 KB Output is correct
62 Correct 642 ms 62648 KB Output is correct
63 Correct 735 ms 64364 KB Output is correct
64 Correct 700 ms 63324 KB Output is correct
65 Correct 569 ms 60008 KB Output is correct