Submission #680342

# Submission time Handle Problem Language Result Execution time Memory
680342 2023-01-10T15:40:23 Z rainboy Candies (JOI18_candies) C
100 / 100
108 ms 8416 KB
#include <stdio.h>

#define N	200000

int ll[N], rr[N]; long long aa[N];
int iq[N + 1], pq[N], cnt;

int lt(int i, int j) { return aa[i] > aa[j]; }

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add(int i) {
	pq[i] = ++cnt, pq_up(i);
}

int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

void pq_remove(int i) {
	int j = iq[cnt--];

	if (j != i)
		pq[j] = pq[i], pq_up(j), pq_dn(j);
	pq[i] = 0;
}

int main() {
	int n, k, i, l, r;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%lld", &aa[i]);
	for (i = 0; i < n; i++)
		ll[i] = i, rr[i] = i;
	for (i = 0; i < n; i++)
		pq_add(i);
	ans = 0;
	for (k = 1; k * 2 - 1 <= n; k++) {
		i = pq_remove_first(), l = i == 0 ? -1 : ll[i - 1], r = rr[i] + 1 == n ? n : rr[rr[i] + 1];
		printf("%lld\n", ans += aa[i]);
		if (l >= 0)
			pq_remove(l);
		if (r < n)
			pq_remove(ll[r]);
		if (l >= 0 && r < n)
			aa[l] = aa[l] - aa[i] + aa[ll[r]], pq_add(l);
		if (l >= 0)
			rr[l] = r;
		if (r < n)
			ll[r] = l;
	}
	return 0;
}

Compilation message

candies.c: In function 'main':
candies.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
candies.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |   scanf("%lld", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 304 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 304 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 108 ms 8368 KB Output is correct
22 Correct 92 ms 8284 KB Output is correct
23 Correct 93 ms 8352 KB Output is correct
24 Correct 99 ms 8240 KB Output is correct
25 Correct 69 ms 8144 KB Output is correct
26 Correct 72 ms 8220 KB Output is correct
27 Correct 77 ms 8300 KB Output is correct
28 Correct 80 ms 8384 KB Output is correct
29 Correct 94 ms 8368 KB Output is correct
30 Correct 73 ms 8396 KB Output is correct
31 Correct 80 ms 8304 KB Output is correct
32 Correct 79 ms 8360 KB Output is correct
33 Correct 82 ms 8164 KB Output is correct
34 Correct 78 ms 8132 KB Output is correct
35 Correct 101 ms 8140 KB Output is correct
36 Correct 94 ms 8400 KB Output is correct
37 Correct 103 ms 8376 KB Output is correct
38 Correct 94 ms 8268 KB Output is correct
39 Correct 68 ms 8140 KB Output is correct
40 Correct 65 ms 8228 KB Output is correct
41 Correct 80 ms 8208 KB Output is correct
42 Correct 76 ms 8416 KB Output is correct
43 Correct 74 ms 8352 KB Output is correct
44 Correct 71 ms 8356 KB Output is correct
45 Correct 75 ms 8348 KB Output is correct
46 Correct 72 ms 8336 KB Output is correct
47 Correct 72 ms 8380 KB Output is correct
48 Correct 86 ms 8140 KB Output is correct
49 Correct 79 ms 8176 KB Output is correct
50 Correct 105 ms 8184 KB Output is correct