結果

問題 No.953 席
ユーザー maspymaspy
提出日時 2020-03-22 03:17:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 858 ms / 2,000 ms
コード長 3,397 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 40,756 KB
最終ジャッジ日時 2023-08-25 22:54:54
合計ジャッジ時間 18,878 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
9,052 KB
testcase_01 AC 17 ms
9,004 KB
testcase_02 AC 589 ms
33,996 KB
testcase_03 AC 577 ms
33,488 KB
testcase_04 AC 587 ms
33,624 KB
testcase_05 AC 497 ms
33,084 KB
testcase_06 AC 590 ms
33,984 KB
testcase_07 AC 559 ms
34,772 KB
testcase_08 AC 763 ms
38,720 KB
testcase_09 AC 457 ms
35,000 KB
testcase_10 AC 168 ms
18,752 KB
testcase_11 AC 556 ms
30,324 KB
testcase_12 AC 774 ms
37,268 KB
testcase_13 AC 840 ms
40,476 KB
testcase_14 AC 727 ms
36,680 KB
testcase_15 AC 700 ms
35,608 KB
testcase_16 AC 766 ms
36,520 KB
testcase_17 AC 650 ms
37,172 KB
testcase_18 AC 649 ms
36,656 KB
testcase_19 AC 758 ms
38,812 KB
testcase_20 AC 649 ms
36,660 KB
testcase_21 AC 579 ms
34,044 KB
testcase_22 AC 784 ms
37,652 KB
testcase_23 AC 651 ms
34,984 KB
testcase_24 AC 666 ms
35,056 KB
testcase_25 AC 858 ms
40,756 KB
testcase_26 AC 446 ms
33,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
from heapq import heappop, heappush, heapify
from collections import defaultdict

N, K1, K2 = map(int, readline().split())
Q = int(readline())
priority = [0] * (N + 2)
if K1 < K2:
    for i in range(K1):
        priority[i] = 2 * (K1 - i)
    for i in range(K2, N + 1):
        priority[i] = 2 * (i - K2) + 1
else:
    for i in range(K2 + 1):
        priority[i] = 2 * (K2 - i) + 1
    for i in range(K1, N + 1):
        priority[i] = 2 * (i - K1)

INF = max(priority) + 1
priority[-1] = INF + 1
priority[0] = INF
p_to_i = [0] * (max(priority) + 1)
for i, x in enumerate(priority):
    p_to_i[x] = i


class RemovableHeap():
    def __init__(self, data):
        self.n_elem = len(data)
        self.data = data
        heapify(self.data)
        self.counter = defaultdict(int)
        for x in data:
            self.counter[x] += 1

    def top(self):
        while True:
            x = self.data[0]
            if not self.counter[x]:
                heappop(self.data)
                continue
            return x

    def push(self, x):
        self.counter[x] += 1
        heappush(self.data, x)
        self.n_elem += 1

    def pop(self):
        while True:
            x = heappop(self.data)
            if self.counter[x]:
                self.counter[x] -= 1
                self.n_elem -= 1
                return x

    def remove(self, x):
        self.counter[x] -= 1
        self.n_elem -= 1

    def empty(self):
        return self.n_elem == 0


filled = [False] * (N + 10)
seats_0 = RemovableHeap(priority[1:N + 1])
seats_1 = RemovableHeap([])

m = map(int, read().split())
A, B = zip(*zip(m, m))
INF = 10 ** 18
event = [(a, 1, i) for i, a in enumerate(A)]
heapify(event)
waiting = deque()
answer = [0] * Q
now_t = 0


def resolve_waiting():
    while waiting:
        if not seats_0.empty():
            p = seats_0.pop()
        elif not seats_1.empty():
            p = seats_1.pop()
        else:
            return
        k = waiting.popleft()
        s = p_to_i[p]
        answer[k] = s
        filled[s] = True
        heappush(event, (now_t + B[k], 0, k))
        if (s > 1) and not (filled[s - 1] or filled[s - 2]):
            seats_0.remove(priority[s - 1])
            seats_1.push(priority[s - 1])
        if s < N and not (filled[s + 1] or filled[s + 2]):
            seats_0.remove(priority[s + 1])
            seats_1.push(priority[s + 1])


def go_home(t, i):
    s = answer[i]
    filled[s] = False
    if filled[s - 1] or filled[s + 1]:
        seats_1.push(priority[s])
    else:
        seats_0.push(priority[s])
    if s > 1 and not (filled[s - 1] or filled[s - 2]):
        seats_0.push(priority[s - 1])
        seats_1.remove(priority[s - 1])
    if s < N and not (filled[s + 1] or filled[s + 2]):
        seats_0.push(priority[s + 1])
        seats_1.remove(priority[s + 1])
    return


while event:
    t, q, i = heappop(event)
    now_t = t
    if q:
        # 到着
        waiting.append(i)
        resolve_waiting()
        continue
    # まとめて帰宅
    go_home(t, i)
    while event:
        t, q, i = event[0]
        if t > now_t or q == 1:
            break
        heappop(event)
        go_home(t, i)
    resolve_waiting()

print('\n'.join(map(str, answer)))
0