結果

問題 No.2740 Old Maid
ユーザー ThetaTheta
提出日時 2024-04-23 18:25:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,300 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 219,748 KB
最終ジャッジ日時 2024-04-23 18:26:23
合計ジャッジ時間 54,990 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,511 ms
219,152 KB
testcase_01 AC 1,522 ms
219,284 KB
testcase_02 AC 1,505 ms
218,516 KB
testcase_03 AC 1,543 ms
219,368 KB
testcase_04 AC 1,760 ms
218,996 KB
testcase_05 AC 1,513 ms
219,748 KB
testcase_06 AC 1,489 ms
219,148 KB
testcase_07 AC 1,554 ms
219,256 KB
testcase_08 AC 1,514 ms
218,760 KB
testcase_09 AC 1,507 ms
218,716 KB
testcase_10 AC 1,464 ms
219,280 KB
testcase_11 AC 1,071 ms
181,008 KB
testcase_12 AC 1,522 ms
169,308 KB
testcase_13 AC 1,679 ms
180,784 KB
testcase_14 AC 441 ms
96,572 KB
testcase_15 AC 585 ms
104,636 KB
testcase_16 AC 1,160 ms
145,508 KB
testcase_17 AC 408 ms
96,816 KB
testcase_18 AC 1,574 ms
176,148 KB
testcase_19 AC 684 ms
113,676 KB
testcase_20 AC 1,958 ms
190,700 KB
testcase_21 AC 486 ms
98,692 KB
testcase_22 AC 1,159 ms
144,348 KB
testcase_23 AC 359 ms
92,756 KB
testcase_24 AC 709 ms
111,376 KB
testcase_25 TLE -
testcase_26 AC 132 ms
80,512 KB
testcase_27 AC 315 ms
88,144 KB
testcase_28 AC 1,172 ms
146,076 KB
testcase_29 AC 1,134 ms
142,792 KB
testcase_30 AC 213 ms
83,948 KB
testcase_31 TLE -
testcase_32 AC 746 ms
115,840 KB
testcase_33 TLE -
testcase_34 AC 1,103 ms
141,884 KB
testcase_35 AC 604 ms
109,732 KB
testcase_36 AC 684 ms
112,224 KB
testcase_37 AC 917 ms
129,488 KB
testcase_38 AC 590 ms
107,720 KB
testcase_39 AC 394 ms
94,484 KB
testcase_40 AC 1,397 ms
162,616 KB
testcase_41 AC 1,870 ms
188,516 KB
testcase_42 AC 59 ms
67,456 KB
testcase_43 AC 62 ms
68,608 KB
testcase_44 AC 59 ms
67,840 KB
testcase_45 AC 62 ms
68,244 KB
testcase_46 AC 63 ms
68,736 KB
testcase_47 AC 59 ms
67,456 KB
testcase_48 AC 66 ms
67,840 KB
testcase_49 AC 63 ms
68,224 KB
testcase_50 AC 66 ms
68,224 KB
testcase_51 AC 73 ms
68,096 KB
testcase_52 AC 68 ms
68,096 KB
testcase_53 AC 69 ms
68,096 KB
testcase_54 AC 59 ms
67,584 KB
testcase_55 AC 69 ms
68,224 KB
testcase_56 AC 59 ms
67,328 KB
testcase_57 AC 57 ms
67,328 KB
testcase_58 AC 60 ms
68,352 KB
testcase_59 AC 61 ms
68,096 KB
testcase_60 AC 62 ms
67,584 KB
testcase_61 AC 66 ms
67,840 KB
testcase_62 AC 67 ms
67,584 KB
testcase_63 AC 59 ms
67,456 KB
testcase_64 AC 57 ms
67,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heapify, heappop
from itertools import pairwise
import sys
from bisect import bisect_left, bisect_right
# https://github.com/tatyam-prime/SortedSet/blob/main/SortedSet.py
import math
from collections import defaultdict
from typing import Generic, Iterable, Iterator, List, Tuple, TypeVar, Optional, Hashable


T = TypeVar('T')


def printe(*args, end="\n", **kwargs):
    print(*args, end=end, file=sys.stderr, **kwargs)


T_ = TypeVar('T_', bound=Hashable)


class RemovableHeapQueue:
    def __init__(self, seq: list[T_] = None) -> None:
        self.__queue: list[T_] = []
        if seq is not None:
            self.__queue.extend(seq)
            heapify(self.__queue)
        self.__remove_queue: dict[T_, int] = defaultdict(int)
        self.__length = len(self.__queue)

    def pop(self) -> T_:
        while self.__queue:
            value = heappop(self.__queue)
            if self.__remove_queue[value] > 0:
                self.__remove_queue[value] -= 1
                continue
            self.__length -= 1
            return value
        raise IndexError

    def push(self, item: T_) -> None:
        self.__length += 1
        if self.__remove_queue[item] > 0:
            self.__remove_queue[item] -= 1
            return
        heappush(self.__queue, item)

    @property
    def top(self) -> T_:
        while self.__queue:
            value = self.__queue[0]
            if self.__remove_queue[value] > 0:

                heappop(self.__queue)
                self.__remove_queue[value] -= 1
                continue
            return value
        raise IndexError

    def remove(self, item: T_) -> None:
        self.__remove_queue[item] += 1
        self.__length -= 1

    def __len__(self) -> int:
        return self.__length


def main():
    N = int(input())
    p = list(map(int, input().split()))
    next_idxs = list(range(1, N + 1))
    next_idxs[-1] = -1
    prev_idxs = list(range(-1, N - 1))

    pairs_set = RemovableHeapQueue()
    for idx, (pair_1, pair_2) in enumerate(pairwise(p)):
        pairs_set.push((pair_1, pair_2, idx, idx + 1))

    q = []
    while pairs_set and len(q) < N:
        used = pairs_set.pop()
        q.extend(used[:2])
        idx_1 = used[2]
        idx_2 = used[3]
        if prev_idxs[idx_1] != -1:
            pairs_set.remove(
                (p[prev_idxs[idx_1]], p[idx_1], prev_idxs[idx_1], idx_1))
        if next_idxs[idx_2] != -1:
            pairs_set.remove(
                (p[idx_2], p[next_idxs[idx_2]], idx_2, next_idxs[idx_2]))

        if prev_idxs[idx_1] != -1 and next_idxs[idx_2] != -1:
            pairs_set.push((p[prev_idxs[idx_1]],
                           p[next_idxs[idx_2]],
                           prev_idxs[idx_1],
                           next_idxs[idx_2]))

        # 前後関係を更新する
        if prev_idxs[idx_1] != -1:
            if next_idxs[idx_2] != -1:
                next_idxs[prev_idxs[idx_1]] = next_idxs[idx_2]
            else:
                next_idxs[prev_idxs[idx_1]] = -1
        if next_idxs[idx_2] != -1:
            if prev_idxs[idx_1] != -1:
                prev_idxs[next_idxs[idx_2]] = prev_idxs[idx_1]
            else:
                prev_idxs[next_idxs[idx_2]] = -1
    print(*q)


if __name__ == "__main__":
    main()
0