답안 #875895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875895 2023-11-20T17:34:35 Z rainboy Potatoes and fertilizers (LMIO19_bulves) C
100 / 100
289 ms 30548 KB
#include <stdio.h>

#define N	500000

long long min(long long a, long long b) { return a < b ? a : b; }

long long xx[(N + 1) * 2]; int iq[2][(N + 1) * 2 + 1], pq[2][(N + 1) * 2], cnt[2], mode;

int lt(int i, int j) {
	return mode == 0 ? xx[i] > xx[j] : xx[i] < xx[j];
}

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

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

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

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

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

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

int pq_first(int mode_) {
	mode = mode_;
	return iq[mode][1];
}

void pq_remove_first(int mode_) {
	int i, j;

	mode = mode_;
	i = iq[mode][1], j = iq[mode][cnt[mode]--];
	if (j != i)
		pq[mode][j] = 1, pq_dn(j);
	pq[mode][i] = 0;
}

int main() {
	int n, i, i_;
	long long xl, xr, x, x_, ans;

	scanf("%d", &n);
	xl = xr = 0, ans = 0;
	xx[0 << 1 | 0] = 0, pq_add(0, 0 << 1 | 0);
	xx[0 << 1 | 1] = 0, pq_add(1, 0 << 1 | 1);
	for (i = 1; i <= n; i++) {
		int a, b;

		scanf("%d%d", &a, &b);
		xl -= b, xr += a - b;
		x_ = min(xr, 0);
		if ((x = xx[i_ = pq_first(0)] + xl) > 0) {
			ans += x, pq_remove_first(0), xx[i_] += xl - xr, pq_add(1, i_);
			xx[i << 1 | 0] = x_ - xl, pq_add(0, i << 1 | 0);
			xx[i << 1 | 1] = x_ - xl, pq_add(0, i << 1 | 1);
		} else if ((x = xx[i_ = pq_first(1)] + xr) < 0) {
			ans += -x, pq_remove_first(1), xx[i_] += xr - xl, pq_add(0, i_);
			xx[i << 1 | 0] = x_ - xr, pq_add(1, i << 1 | 0);
			xx[i << 1 | 1] = x_ - xr, pq_add(1, i << 1 | 1);
		} else {
			xx[i << 1 | 0] = x_ - xl, pq_add(0, i << 1 | 0);
			xx[i << 1 | 1] = x_ - xr, pq_add(1, i << 1 | 1);
		}
	}
	while (cnt[0] && (x = xx[pq_first(0)] + xl) > 0)
		ans += x, pq_remove_first(0);
	while (cnt[1] && (x = xx[pq_first(1)] + xr) < 0)
		ans += -x, pq_remove_first(1);
	printf("%lld\n", ans);
	return 0;
}

Compilation message

bulves.c: In function 'main':
bulves.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
bulves.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d%d", &a, &b);
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 17 ms 9036 KB Output is correct
5 Correct 38 ms 9564 KB Output is correct
6 Correct 125 ms 18952 KB Output is correct
7 Correct 112 ms 30548 KB Output is correct
8 Correct 215 ms 28768 KB Output is correct
9 Correct 147 ms 27776 KB Output is correct
10 Correct 149 ms 25720 KB Output is correct
11 Correct 162 ms 25428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 17 ms 9036 KB Output is correct
5 Correct 38 ms 9564 KB Output is correct
6 Correct 125 ms 18952 KB Output is correct
7 Correct 112 ms 30548 KB Output is correct
8 Correct 215 ms 28768 KB Output is correct
9 Correct 147 ms 27776 KB Output is correct
10 Correct 149 ms 25720 KB Output is correct
11 Correct 162 ms 25428 KB Output is correct
12 Correct 57 ms 12884 KB Output is correct
13 Correct 148 ms 21656 KB Output is correct
14 Correct 119 ms 30468 KB Output is correct
15 Correct 214 ms 28764 KB Output is correct
16 Correct 289 ms 28100 KB Output is correct
17 Correct 100 ms 25620 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8680 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8672 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 17 ms 9036 KB Output is correct
12 Correct 38 ms 9564 KB Output is correct
13 Correct 125 ms 18952 KB Output is correct
14 Correct 112 ms 30548 KB Output is correct
15 Correct 215 ms 28768 KB Output is correct
16 Correct 147 ms 27776 KB Output is correct
17 Correct 149 ms 25720 KB Output is correct
18 Correct 162 ms 25428 KB Output is correct
19 Correct 57 ms 12884 KB Output is correct
20 Correct 148 ms 21656 KB Output is correct
21 Correct 119 ms 30468 KB Output is correct
22 Correct 214 ms 28764 KB Output is correct
23 Correct 289 ms 28100 KB Output is correct
24 Correct 100 ms 25620 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8680 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8672 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8540 KB Output is correct
32 Correct 2 ms 8540 KB Output is correct
33 Correct 40 ms 13188 KB Output is correct
34 Correct 103 ms 21580 KB Output is correct
35 Correct 184 ms 30288 KB Output is correct
36 Correct 179 ms 27732 KB Output is correct
37 Correct 159 ms 28764 KB Output is correct
38 Correct 162 ms 30508 KB Output is correct
39 Correct 96 ms 26780 KB Output is correct
40 Correct 122 ms 25940 KB Output is correct
41 Correct 104 ms 25752 KB Output is correct
42 Correct 113 ms 25536 KB Output is correct
43 Correct 131 ms 25532 KB Output is correct
44 Correct 137 ms 25440 KB Output is correct
45 Correct 235 ms 30072 KB Output is correct
46 Correct 77 ms 25936 KB Output is correct
47 Correct 124 ms 22736 KB Output is correct