結果

問題 No.2667 Constrained Permutation
ユーザー ShirotsumeShirotsume
提出日時 2023-12-30 01:41:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,391 ms / 2,000 ms
コード長 1,172 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 150,960 KB
最終ジャッジ日時 2024-01-15 12:27:07
合計ジャッジ時間 30,578 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
56,140 KB
testcase_01 AC 41 ms
56,140 KB
testcase_02 AC 48 ms
65,904 KB
testcase_03 AC 56 ms
70,864 KB
testcase_04 AC 80 ms
77,588 KB
testcase_05 AC 46 ms
65,968 KB
testcase_06 AC 85 ms
77,460 KB
testcase_07 AC 44 ms
60,248 KB
testcase_08 AC 98 ms
77,544 KB
testcase_09 AC 83 ms
77,680 KB
testcase_10 AC 53 ms
68,404 KB
testcase_11 AC 76 ms
77,460 KB
testcase_12 AC 474 ms
100,072 KB
testcase_13 AC 96 ms
78,176 KB
testcase_14 AC 1,238 ms
147,396 KB
testcase_15 AC 989 ms
131,536 KB
testcase_16 AC 691 ms
114,224 KB
testcase_17 AC 290 ms
88,452 KB
testcase_18 AC 1,391 ms
150,960 KB
testcase_19 AC 1,296 ms
147,244 KB
testcase_20 AC 1,317 ms
148,524 KB
testcase_21 AC 1,234 ms
147,760 KB
testcase_22 AC 1,254 ms
147,624 KB
testcase_23 AC 1,250 ms
147,756 KB
testcase_24 AC 555 ms
105,844 KB
testcase_25 AC 1,039 ms
135,996 KB
testcase_26 AC 1,283 ms
150,724 KB
testcase_27 AC 1,269 ms
150,792 KB
testcase_28 AC 842 ms
132,084 KB
testcase_29 AC 819 ms
131,008 KB
testcase_30 AC 908 ms
134,620 KB
testcase_31 AC 563 ms
113,336 KB
testcase_32 AC 166 ms
83,168 KB
testcase_33 AC 1,182 ms
145,784 KB
testcase_34 AC 663 ms
113,204 KB
testcase_35 AC 766 ms
118,020 KB
testcase_36 AC 739 ms
115,380 KB
testcase_37 AC 792 ms
121,136 KB
testcase_38 AC 923 ms
126,516 KB
testcase_39 AC 101 ms
78,304 KB
testcase_40 AC 558 ms
97,716 KB
testcase_41 AC 909 ms
114,508 KB
testcase_42 AC 630 ms
114,480 KB
testcase_43 AC 881 ms
131,092 KB
testcase_44 AC 429 ms
100,960 KB
testcase_45 AC 108 ms
78,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353
import heapq
n = ii()

LR = []
lr = []
for i in range(n):
    l, r = mi()
    lr.append((l, r))
    LR.append((l, -1))
    LR.append((r, 1))
LR.sort()

LR2 = [[0, 0] for _ in range(n)]
lcnt = 0
rcnt = 0
for v, x in LR:
    if x == -1:
        LR2[lcnt][0] = v
        lcnt += 1
    else:
        LR2[rcnt][1] = v
        rcnt += 1
    
mink = -inf
maxk = inf
LR2 = LR2[::]
for i in range(n):
    l, r = LR2[i]
    mink = max(mink, l - i)
    maxk = min(maxk, r - i)
ans = max(0, maxk - mink + 1)
now = mink
cnt = 0
q = []

graph =[[] for _ in range(n)]

for l, r in lr:
    if l < now:
        graph[0].append(r)
    elif now <= l <= now + n - 1:
        graph[l - now].append(r)
    else:
        ans = 0
        break
for i in range(n):
    for v in graph[i]:
        heapq.heappush(q, v)
    to = heapq.heappop(q)
    if to < now:
        ans = 0
        break
    now += 1

print(ans)
        

        
        
    
    
0