結果

問題 No.2667 Constrained Permutation
ユーザー 👑 rin204rin204
提出日時 2024-03-08 21:58:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 495 ms / 2,000 ms
コード長 752 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 119,052 KB
最終ジャッジ日時 2024-03-08 21:58:14
合計ジャッジ時間 12,910 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,460 KB
testcase_01 AC 40 ms
53,460 KB
testcase_02 AC 52 ms
63,236 KB
testcase_03 AC 65 ms
67,872 KB
testcase_04 AC 93 ms
76,620 KB
testcase_05 AC 49 ms
61,128 KB
testcase_06 AC 90 ms
76,620 KB
testcase_07 AC 70 ms
55,600 KB
testcase_08 AC 91 ms
76,876 KB
testcase_09 AC 93 ms
76,880 KB
testcase_10 AC 53 ms
59,696 KB
testcase_11 AC 86 ms
77,008 KB
testcase_12 AC 209 ms
87,312 KB
testcase_13 AC 92 ms
76,844 KB
testcase_14 AC 359 ms
108,696 KB
testcase_15 AC 348 ms
95,052 KB
testcase_16 AC 293 ms
90,768 KB
testcase_17 AC 168 ms
81,416 KB
testcase_18 AC 371 ms
105,992 KB
testcase_19 AC 495 ms
100,040 KB
testcase_20 AC 476 ms
102,928 KB
testcase_21 AC 432 ms
101,252 KB
testcase_22 AC 452 ms
101,192 KB
testcase_23 AC 462 ms
101,196 KB
testcase_24 AC 213 ms
94,136 KB
testcase_25 AC 285 ms
108,812 KB
testcase_26 AC 333 ms
119,052 KB
testcase_27 AC 363 ms
110,672 KB
testcase_28 AC 199 ms
83,344 KB
testcase_29 AC 194 ms
82,968 KB
testcase_30 AC 208 ms
83,128 KB
testcase_31 AC 157 ms
80,232 KB
testcase_32 AC 113 ms
76,724 KB
testcase_33 AC 384 ms
100,076 KB
testcase_34 AC 158 ms
79,960 KB
testcase_35 AC 325 ms
91,008 KB
testcase_36 AC 268 ms
90,336 KB
testcase_37 AC 279 ms
92,164 KB
testcase_38 AC 402 ms
93,368 KB
testcase_39 AC 110 ms
76,720 KB
testcase_40 AC 159 ms
80,196 KB
testcase_41 AC 214 ms
82,724 KB
testcase_42 AC 298 ms
93,768 KB
testcase_43 AC 319 ms
100,756 KB
testcase_44 AC 211 ms
85,776 KB
testcase_45 AC 108 ms
76,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *

n = int(input())
L = [0] * n
R = [0] * n
for i in range(n):
    L[i], R[i] = map(int, input().split())


R_ = sorted(R)
ma = min(r - i for i, r in enumerate(R_))
L_ = sorted(L)
mi = max(l - i for i, l in enumerate(L_))


def ok(x):
    for i in range(n):
        L[i] -= x
        R[i] -= x
        L[i] = max(L[i], 0)
        R[i] = min(R[i], n - 1)

    add = [[] for _ in range(n)]
    for l, r in zip(L, R):
        add[l].append(r)

    hq = []
    for i in range(n):
        for x in add[i]:
            heappush(hq, x)
        while hq and hq[0] < i:
            heappop(hq)
        if not hq:
            return False

        heappop(hq)
    return True


if mi <= ma and ok(ma):
    print(ma - mi + 1)
else:
    print(0)
0