제출 #683621

#제출 시각아이디문제언어결과실행 시간메모리
683621rainboyArranging Tickets (JOI17_arranging_tickets)C11
0 / 100
1 ms212 KiB
#include <stdio.h>
#include <string.h>

#define N	100000

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int xx[N * 2];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			if (xx[ii[j]] == xx[i_])
				j++;
			else if (xx[ii[j]] < xx[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int iq[N + 1], pq[N], 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) {
	iq[pq[i] = ++cnt] = i, 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 cc[N], ii[N * 2];
	int n, m, h, h_, h1, i, a, tmp, d, k, k_;

	scanf("%d%d", &m, &n);
	for (i = 0; i < n; i++) {
		scanf("%d%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1], &cc[i]), xx[i << 1 | 0]--, xx[i << 1 | 1]--;
		if (xx[i << 1 | 0] > xx[i << 1 | 1])
			tmp = xx[i << 1 | 0], xx[i << 1 | 0] = xx[i << 1 | 1], xx[i << 1 | 1] = tmp;
	}
	k_ = n + 1;
	for (a = 0; a < m; a++) {
		for (i = 0; i < n; i++) {
			xx[i << 1 | 0]--, xx[i << 1 | 1]--;
			if (xx[i << 1 | 0] < 0)
				tmp = m + xx[i << 1 | 0], xx[i << 1 | 0] = xx[i << 1 | 1], xx[i << 1 | 1] = tmp;
		}
		for (i = 0; i < n * 2; i++)
			ii[i] = i;
		sort(ii, 0, n * 2);
		memset(iq, 0, n * sizeof *iq), cnt = 0;
		d = 0, k = 0;
		for (h = 0; h < n * 2; h = h_) {
			h_ = h + 1;
			while (h_ < n * 2 && xx[ii[h_]] == xx[ii[h]])
				h_++;
			for (h1 = h; h1 < h_; h1++) {
				i = ii[h1];
				if ((i & 1) == 0)
					pq_add(i >> 1), d++;
				else {
					if (iq[i >> 1])
						pq_remove(i >> 1);
					d--;
				}
			}
			while (cnt > d / 2)
				pq_remove_first(), k++;
		}
		k_ = min(k_, k);
	}
	printf("%d\n", k_);
	return 0;
}

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

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