This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* https://www.ioi-jp.org/camp/2017/2017-sp-tasks/2017-sp-d2-arranging_tickets-review.pdf */
#include <stdio.h>
#include <string.h>
#define N 200000
#define M 100000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ll[M], rr[M], cc[M], cc_[M], xx[M * 2], hh[M * 2];
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[hh[j]] == xx[h])
j++;
else if (xx[hh[j]] < xx[h]) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
sort(hh, l, i);
l = k;
}
}
int iq[M + 1], pq[M], cnt;
int lt(int i, int j) {
return xx[i << 1 | 1] > xx[j << 1 | 1];
}
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_first() {
return iq[1];
}
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() {
static long long dd[N];
int n, m, h, h_, i, i_, r, tmp;
long long c, c_, d, x, ans;
scanf("%d%d", &n, &m);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &ll[h], &rr[h], &cc[h]), ll[h]--, rr[h]--;
if (ll[h] > rr[h])
tmp = ll[h], ll[h] = rr[h], rr[h] = tmp;
dd[ll[h]] += cc[h], dd[rr[h]] -= cc[h];
}
for (i = 1; i < n; i++)
dd[i] += dd[i - 1];
i_ = -1;
for (i = 0; i < n; i++)
if (i_ == -1 || dd[i_] < dd[i])
i_ = i;
for (h = 0; h < m; h++) {
ll[h] = (ll[h] - i_ - 1 + n) % n;
rr[h] = (rr[h] - i_ - 1 + n) % n;
if (ll[h] > rr[h])
tmp = ll[h], ll[h] = rr[h], rr[h] = tmp;
xx[h << 1 | 0] = ll[h], xx[h << 1 | 1] = rr[h];
}
for (h = 0; h < m * 2; h++)
hh[h] = h;
sort(hh, 0, m * 2);
ans = INF;
for (r = 0; r < 2; r++) {
memset(pq, 0, m * sizeof *pq), cnt = 0;
memcpy(cc_, cc, m * sizeof *cc);
c = 0, d = 0, x = 0;
for (h = 0; h < m * 2; h++) {
h_ = hh[h];
if ((h_ & 1) == 0)
pq_add(h_ >> 1), c += cc_[h_ >> 1], d += cc[h_ >> 1];
else {
if (pq[h_ >> 1])
pq_remove(h_ >> 1);
c -= cc_[h_ >> 1], d -= cc[h_ >> 1];
}
if (h + 1 == m * 2 || xx[hh[h + 1]] != xx[h_]) {
while ((c_ = c - (d + r) / 2) > 0)
if (c_ >= cc_[h_ = pq_first()])
c -= cc_[h_], x += cc_[h_], cc_[h_] = 0, pq_remove(h_);
else
c -= c_, x += c_, cc_[h_] -= c_;
}
}
ans = min(ans, x + r);
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
arranging_tickets.c: In function 'main':
arranging_tickets.c:85:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
arranging_tickets.c:87:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d%d%d", &ll[h], &rr[h], &cc[h]), ll[h]--, rr[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |