답안 #875889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875889 2023-11-20T17:15:23 Z rainboy Potatoes and fertilizers (LMIO19_bulves) C
0 / 100
2 ms 8632 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, 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;
		if ((x = xx[i_ = pq_first(0)] + xl) > 0)
			ans += x, pq_remove_first(0), xx[i_] += xl - xr, pq_add(1, i_);
		else if ((x = xx[i_ = pq_first(1)] + xr) < 0)
			ans += -x, pq_remove_first(1), xx[i_] += xr - xl, pq_add(0, i_);
		x = min(xr, 0);
		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 2 ms 8632 KB Output is correct
3 Incorrect 2 ms 8632 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 2 ms 8632 KB Output is correct
3 Incorrect 2 ms 8632 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 2 ms 8632 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 2 ms 8632 KB Output is correct
3 Incorrect 2 ms 8632 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 2 ms 8632 KB Output is correct
3 Incorrect 2 ms 8632 KB Output isn't correct
4 Halted 0 ms 0 KB -