This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
Compilation message (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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |