結果

問題 No.953 席
ユーザー mkawa2mkawa2
提出日時 2019-12-22 17:37:20
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 1,136 ms / 2,000 ms
コード長 3,062 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 11,136 KB
実行使用メモリ 36,916 KB
最終ジャッジ日時 2023-10-12 13:31:36
合計ジャッジ時間 26,319 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,472 KB
testcase_01 AC 17 ms
8,360 KB
testcase_02 AC 862 ms
30,400 KB
testcase_03 AC 812 ms
29,716 KB
testcase_04 AC 861 ms
30,140 KB
testcase_05 AC 692 ms
29,328 KB
testcase_06 AC 858 ms
30,280 KB
testcase_07 AC 653 ms
33,900 KB
testcase_08 AC 915 ms
35,472 KB
testcase_09 AC 518 ms
33,232 KB
testcase_10 AC 181 ms
16,748 KB
testcase_11 AC 631 ms
28,728 KB
testcase_12 AC 1,001 ms
33,960 KB
testcase_13 AC 979 ms
36,316 KB
testcase_14 AC 996 ms
32,840 KB
testcase_15 AC 987 ms
32,356 KB
testcase_16 AC 1,000 ms
32,704 KB
testcase_17 AC 1,027 ms
34,608 KB
testcase_18 AC 992 ms
33,524 KB
testcase_19 AC 1,136 ms
36,916 KB
testcase_20 AC 1,019 ms
33,476 KB
testcase_21 AC 970 ms
30,652 KB
testcase_22 AC 996 ms
34,332 KB
testcase_23 AC 943 ms
31,392 KB
testcase_24 AC 938 ms
31,440 KB
testcase_25 AC 928 ms
36,588 KB
testcase_26 AC 558 ms
29,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    def sitdown(i, j, a, b):
        used_seat[j] = True
        ans[i] = j
        heappush(timeline,[a + b, 0, j, -1])
        return

    n, k1, k2 = MI()
    q = int(input())
    timeline = []
    for i in range(q):
        a, b = MI()
        # 時刻aに2(来店)したiさんがb時間滞在
        heappush(timeline, [a, 2, i, b])
    # jは席番号でdist[j]は入口からの距離
    # k1,k2を1:2に内分する点に入口を設定
    # 距離は分母3の分数になるので3倍して整数に
    dist = [abs(2 * k1 + k2 - 3 * j) for j in range(n + 2)]
    #print(dist)
    used_seat = [False] * (n + 2)
    ans = [-1] * q
    # nosideが隣に人がいない席 onsideがいる席
    noside = [[dist[j], j] for j in range(1, n + 1)]
    heapify(noside)
    onside = []
    wait = []
    while timeline:
        a, e, i, b = heappop(timeline)
        #print(timeline,a, e, i, b)
        # 来店
        if e:
            finish = False
            # 隣に人がいない席があればそこに座らせ、退店時間をtimelineに追加
            while noside:
                d, j = heappop(noside)
                # 隣にいればその席をonsideに追加
                if used_seat[j - 1] or used_seat[j + 1]:
                    heappush(onside, [d, j])
                else:
                    sitdown(i, j, a, b)
                    finish = True
                    break
            if finish: continue
            # nosideで決まらなければonsideで席を探す
            while onside:
                d, j = heappop(onside)
                if used_seat[j]: continue
                sitdown(i, j, a, b)
                finish = True
                break
            # 見つからなければwaitへ
            if not finish: heappush(wait, [a, e, i, b])
        # 退店
        else:
            j = i
            used_seat[j] = False
            # 待ちがいれば、この時間aに待ちからの復帰1としてtimelineに追加
            if wait:
                _, _, i0, b0 = heappop(wait)
                heappush(timeline, [a, 1, i0, b0])
            # 両隣りが空きかどうかでnosideかonsideに席追加
            if used_seat[j - 1] or used_seat[j + 1]:
                heappush(onside, [dist[j], j])
            else:
                heappush(noside, [dist[j], j])
            # 各隣が両隣り空きになったらnosideに追加
            if j < n and used_seat[j + 1] == False and used_seat[j + 2] == False:
                heappush(noside, [dist[j + 1], j + 1])
            if j - 2 >= 0 and used_seat[j - 1] == False and used_seat[j - 2] == False:
                heappush(noside, [dist[j - 1], j - 1])
    for x in ans:
        print(x)

main()
0