結果

問題 No.2667 Constrained Permutation
ユーザー 👑 ygussanyygussany
提出日時 2024-02-18 10:05:14
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,424 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 13,480 KB
最終ジャッジ日時 2024-02-18 10:05:38
合計ジャッジ時間 10,314 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,480 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 4 ms
6,676 KB
testcase_04 AC 7 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 6 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 7 ms
6,676 KB
testcase_09 AC 7 ms
6,676 KB
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 5 ms
6,676 KB
testcase_12 AC 792 ms
6,676 KB
testcase_13 AC 21 ms
6,676 KB
testcase_14 TLE -
testcase_15 AC 637 ms
6,676 KB
testcase_16 AC 461 ms
6,676 KB
testcase_17 AC 154 ms
6,676 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 854 ms
6,676 KB
testcase_22 AC 861 ms
6,676 KB
testcase_23 AC 831 ms
6,676 KB
testcase_24 AC 1,313 ms
6,676 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
}

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 L = 0, R = 1000000001, 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 {
			L = M;
			R = M;
		}
	}
	if (L > R) {
		printf("0\n");
		return 0;
	}
	
	int LL = L, RR = 1000000001;
	while (LL < RR) {
		M = (LL + RR + 1) / 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) RR = M - 1;
		else LL = M;
	}
	L = 0;
	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) L = M + 1;
		else R = M;
	}
	printf("%d\n", LL - L + 1);
	fflush(stdout);
	return 0;
}
0