結果

問題 No.2667 Constrained Permutation
ユーザー 👑 ygussanyygussany
提出日時 2024-02-18 10:22:25
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,911 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-18 10:22:38
合計ジャッジ時間 11,598 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 4 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 WA -
testcase_08 AC 4 ms
6,676 KB
testcase_09 AC 4 ms
6,676 KB
testcase_10 WA -
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 173 ms
6,676 KB
testcase_13 AC 6 ms
6,676 KB
testcase_14 AC 602 ms
6,676 KB
testcase_15 AC 322 ms
6,676 KB
testcase_16 AC 215 ms
6,676 KB
testcase_17 AC 69 ms
6,676 KB
testcase_18 AC 615 ms
6,676 KB
testcase_19 AC 537 ms
6,676 KB
testcase_20 AC 458 ms
6,676 KB
testcase_21 WA -
testcase_22 AC 358 ms
6,676 KB
testcase_23 WA -
testcase_24 AC 276 ms
6,676 KB
testcase_25 AC 522 ms
6,676 KB
testcase_26 AC 689 ms
6,676 KB
testcase_27 AC 762 ms
6,676 KB
testcase_28 AC 120 ms
6,676 KB
testcase_29 AC 116 ms
6,676 KB
testcase_30 AC 127 ms
6,676 KB
testcase_31 AC 78 ms
6,676 KB
testcase_32 AC 17 ms
6,676 KB
testcase_33 AC 262 ms
6,676 KB
testcase_34 AC 155 ms
6,676 KB
testcase_35 AC 152 ms
6,676 KB
testcase_36 AC 174 ms
6,676 KB
testcase_37 AC 152 ms
6,676 KB
testcase_38 AC 253 ms
6,676 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 121 ms
6,676 KB
testcase_41 AC 255 ms
6,676 KB
testcase_42 AC 232 ms
6,676 KB
testcase_43 AC 327 ms
6,676 KB
testcase_44 AC 213 ms
6,676 KB
testcase_45 AC 6 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[200001];
	int size;
} min_heap;

void push(min_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

void chmin(int *a, int b)
{
	if (*a > b) *a = b;
}

void chmax(int *a, int b)
{
	if (*a < b) *a = b;
}

int main()
{
	int i, n, l[200001], r[200001];
	scanf("%d", &n);
	for (i = 1; i <= n; i++) scanf("%d %d", &(l[i]), &(r[i]));
	
	int k, L = 0, R = 1000000000, M;
	min_heap h[2];
	data d;
	while (L < R) {
		M = (L + R) / 2;
		h[0].size = 0;
		h[1].size = 0;
		for (i = 1; i <= n; i++) {
			d.key = l[i] - M;
			d.id = i;
			push(&(h[0]), d);
		}
		for (i = 1; i <= n; i++) {
			while (h[0].size > 0 && h[0].obj[1].key <= i) {
				d = pop(&(h[0]));
				d.key = r[d.id] - M;
				push(&(h[1]), d);
			}
			if (h[1].size == 0 || h[1].obj[1].key < i) break;
			pop(&(h[1]));
		}
		if (i <= n) {
			if (h[1].size == 0) L = M + 1;
			else R = M - 1;
		} else break;
	}
	if (L >= R) {
		printf("0\n");
		return 0;
	}
	
	L = 0;
	R = 1000000000;
	h[0].size = 0;
	h[1].size = 0;
	for (i = 1; i <= n; i++) {
		d.key = l[i];
		d.id = i;
		push(&(h[0]), d);
		d.key = r[i];
		d.id = i;
		push(&(h[1]), d);
	}
	for (i = 1; i <= n; i++) {
		d = pop(&(h[0]));
		chmax(&L, d.key - i);
		d = pop(&(h[1]));
		chmin(&R, d.key - i);
	}
	printf("%d\n", R - L + 1);
	fflush(stdout);
	return 0;
}
0