Submission #684835

#TimeUsernameProblemLanguageResultExecution timeMemory
684835rainboyArranging Tickets (JOI17_arranging_tickets)C11
85 / 100
94 ms7464 KiB
/* 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 xx[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_remove_first() {
	int i = iq[1], j = iq[cnt--];

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

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 int ll[M], rr[M], cc[M], hh[M * 2];
	static long long dd[N];
	int n, m, h, h_, i, i_, r, tmp;
	long long 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;
		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++, d++;
			else {
				if (pq[h_ >> 1])
					pq_remove(h_ >> 1), c--;
				d--;
			}
			if (h + 1 == m * 2 || xx[hh[h + 1]] != xx[h_]) {
				while (c > (d + r) / 2)
					pq_remove_first(), c--, x++;
			}
		}
		ans = min(ans, x + r);
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

arranging_tickets.c: In function 'main':
arranging_tickets.c:91:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
arranging_tickets.c:93:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%d%d%d", &ll[h], &rr[h], &cc[h]), ll[h]--, rr[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...