제출 #784125

#제출 시각아이디문제언어결과실행 시간메모리
784125rainboySafety (NOI18_safety)C11
100 / 100
70 ms8448 KiB
#include <stdio.h>

#define N	200000
#define INF	0x3f3f3f3f3f3f3f3fLL

long long xx[N * 2]; int iq[2][N * 2 + 1], pq[N * 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[i]; (q = p / 2) && lt(i, j = iq[mode][q]); p = q)
		iq[mode][pq[j] = p] = j;
	iq[mode][pq[i] = p] = i;
}

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

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

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

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

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

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

int main() {
	int n, d, i, j, x;
	long long d_, x1, x2, ans;

	scanf("%d%d", &n, &d);
	ans = 0;
	for (i = 0; i < n; i++) {
		scanf("%d", &x);
		d_ = (long long) d * i;
		mode = 0, x1 = cnt[mode] == 0 ? -INF : xx[pq_first()] - d_;
		mode = 1, x2 = cnt[mode] == 0 ? INF : xx[pq_first()] + d_;
		if (x < x1) {
			ans += x1 - x;
			mode = 0;
			j = pq_remove_first();
			xx[i << 1 | 0] = x + d_, pq_add(i << 1 | 0);
			xx[i << 1 | 1] = x + d_, pq_add(i << 1 | 1);
			mode = 1;
			xx[j] -= d_ * 2, pq_add(j);
		} else if (x > x2) {
			ans += x - x2;
			mode = 1;
			j = pq_remove_first();
			xx[i << 1 | 0] = x - d_, pq_add(i << 1 | 0);
			xx[i << 1 | 1] = x - d_, pq_add(i << 1 | 1);
			mode = 0;
			xx[j] += d_ * 2, pq_add(j);
		} else {
			mode = 0;
			xx[i << 1 | 0] = x + d_, pq_add(i << 1 | 0);
			mode = 1;
			xx[i << 1 | 1] = x - d_, pq_add(i << 1 | 1);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

safety.c: In function 'main':
safety.c:51:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |  scanf("%d%d", &n, &d);
      |  ^~~~~~~~~~~~~~~~~~~~~
safety.c:54:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   scanf("%d", &x);
      |   ^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...