結果

問題 No.2740 Old Maid
ユーザー ThetaTheta
提出日時 2024-04-23 18:25:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,300 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 219,496 KB
最終ジャッジ日時 2024-10-15 19:10:13
合計ジャッジ時間 57,898 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,738 ms
219,280 KB
testcase_01 AC 1,635 ms
218,900 KB
testcase_02 AC 1,561 ms
218,416 KB
testcase_03 AC 1,595 ms
219,124 KB
testcase_04 AC 1,686 ms
218,848 KB
testcase_05 AC 1,599 ms
219,496 KB
testcase_06 AC 1,593 ms
218,904 KB
testcase_07 AC 1,676 ms
218,604 KB
testcase_08 AC 1,565 ms
218,268 KB
testcase_09 AC 1,596 ms
218,372 KB
testcase_10 AC 1,519 ms
219,016 KB
testcase_11 AC 1,095 ms
180,588 KB
testcase_12 AC 1,590 ms
169,288 KB
testcase_13 AC 1,725 ms
180,564 KB
testcase_14 AC 456 ms
96,700 KB
testcase_15 AC 555 ms
104,512 KB
testcase_16 AC 1,198 ms
145,256 KB
testcase_17 AC 432 ms
96,180 KB
testcase_18 AC 1,695 ms
175,856 KB
testcase_19 AC 691 ms
113,560 KB
testcase_20 TLE -
testcase_21 AC 477 ms
98,828 KB
testcase_22 AC 1,180 ms
144,200 KB
testcase_23 AC 370 ms
92,504 KB
testcase_24 AC 684 ms
111,128 KB
testcase_25 TLE -
testcase_26 AC 146 ms
80,128 KB
testcase_27 AC 324 ms
87,500 KB
testcase_28 AC 1,215 ms
145,576 KB
testcase_29 AC 1,152 ms
142,880 KB
testcase_30 AC 207 ms
83,692 KB
testcase_31 TLE -
testcase_32 AC 782 ms
115,588 KB
testcase_33 TLE -
testcase_34 AC 1,114 ms
141,472 KB
testcase_35 AC 669 ms
109,480 KB
testcase_36 AC 681 ms
111,968 KB
testcase_37 AC 946 ms
128,956 KB
testcase_38 AC 605 ms
107,336 KB
testcase_39 AC 412 ms
94,356 KB
testcase_40 AC 1,429 ms
162,600 KB
testcase_41 AC 1,952 ms
187,832 KB
testcase_42 AC 66 ms
67,072 KB
testcase_43 AC 67 ms
67,968 KB
testcase_44 AC 66 ms
67,968 KB
testcase_45 AC 70 ms
67,712 KB
testcase_46 AC 68 ms
67,968 KB
testcase_47 AC 66 ms
67,072 KB
testcase_48 AC 66 ms
67,200 KB
testcase_49 AC 67 ms
67,584 KB
testcase_50 AC 66 ms
67,712 KB
testcase_51 AC 66 ms
67,584 KB
testcase_52 AC 66 ms
67,584 KB
testcase_53 AC 66 ms
67,712 KB
testcase_54 AC 68 ms
67,328 KB
testcase_55 AC 73 ms
67,584 KB
testcase_56 AC 67 ms
66,944 KB
testcase_57 AC 65 ms
67,328 KB
testcase_58 AC 68 ms
67,840 KB
testcase_59 AC 68 ms
67,968 KB
testcase_60 AC 69 ms
66,944 KB
testcase_61 AC 66 ms
67,200 KB
testcase_62 AC 64 ms
67,072 KB
testcase_63 AC 66 ms
67,072 KB
testcase_64 AC 68 ms
66,944 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