結果

問題 No.953 席
ユーザー mkawa2
提出日時 2019-12-22 17:37:20
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,375 ms / 2,000 ms
コード長 3,062 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 39,492 KB
最終ジャッジ日時 2024-09-14 12:30:36
合計ジャッジ時間 30,055 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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