結果

問題 No.759 悪くない忘年会にしような!
ユーザー tktk_snsn
提出日時 2022-05-12 13:08:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,721 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 128,000 KB
最終ジャッジ日時 2024-07-20 06:27:14
合計ジャッジ時間 16,734 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import heapq
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


N = int(input())
PQR = tuple(tuple(map(int, input().split())) for _ in range(N))
P, Q, R = zip(*PQR)

ptoi = {p: i for i, p in enumerate(sorted(set(P)))}
M = len(ptoi)
d = [[] for _ in range(M)]
for i, p in enumerate(P):
    d[ptoi[p]].append(i)

for i in range(M):
    stack = []
    d[i].sort(key=lambda j: (Q[j], R[j]))
    for j in d[i]:
        if stack and R[stack[-1]] < R[j]:
            stack.pop()
        stack.append(j)
    d[i] = stack
    d[i].reverse()


def merge(i, j):
    F = d[i]
    G = d[j]

    stf = []
    stg = []
    while F and G:
        if Q[F[-1]] <= Q[G[-1]]:
            heapq.heappush(stf, (R[F[-1]], F[-1]))
            F.pop()
        else:
            while stf and stf[0][0] <= R[G[-1]]:
                heapq.heappop(stf)
            stg.append(G.pop())
    while F:
        heapq.heappush(stf, (R[F[-1]], F[-1]))
        F.pop()
    while G:
        while stf and stf[0][0] <= R[G[-1]]:
            heapq.heappop(stf)
        stg.append(G.pop())

    stf = [k for _, k in stf]
    stf.sort(key=lambda k: Q[k], reverse=True)
    stg.reverse()
    while stf and stg:
        if Q[stf[-1]] <= Q[stg[-1]]:
            d[i].append(stf.pop())
        else:
            d[i].append(stg.pop())
    while stf:
        d[i].append(stf.pop())
    while stg:
        d[i].append(stg.pop())
    d[i].reverse()


que = deque(range(M))
while len(que) > 1:
    a = que.popleft()
    b = que.popleft()
    if a > b:
        que.append(a)
        que.appendleft(b)
        continue
    merge(a, b)
    que.append(a)


ans = [i + 1 for i in d[0]]
ans.sort()
print(*ans, sep="\n")
0