結果

問題 No.2740 Old Maid
ユーザー ThetaTheta
提出日時 2024-04-23 18:21:33
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,285 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 219,296 KB
最終ジャッジ日時 2024-10-15 19:05:35
合計ジャッジ時間 52,825 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,555 ms
219,296 KB
testcase_01 AC 1,551 ms
218,892 KB
testcase_02 AC 1,547 ms
218,292 KB
testcase_03 AC 1,586 ms
218,992 KB
testcase_04 AC 1,609 ms
218,608 KB
testcase_05 AC 1,580 ms
219,240 KB
testcase_06 AC 1,556 ms
219,024 KB
testcase_07 AC 1,595 ms
218,612 KB
testcase_08 AC 1,570 ms
218,396 KB
testcase_09 AC 1,548 ms
218,124 KB
testcase_10 AC 1,504 ms
219,132 KB
testcase_11 AC 1,096 ms
181,868 KB
testcase_12 AC 1,628 ms
169,036 KB
testcase_13 AC 1,809 ms
181,212 KB
testcase_14 AC 451 ms
96,184 KB
testcase_15 AC 562 ms
104,256 KB
testcase_16 AC 1,218 ms
144,728 KB
testcase_17 AC 434 ms
96,964 KB
testcase_18 AC 1,675 ms
175,852 KB
testcase_19 AC 703 ms
114,084 KB
testcase_20 TLE -
testcase_21 AC 478 ms
98,436 KB
testcase_22 AC 1,188 ms
144,324 KB
testcase_23 AC 362 ms
92,532 KB
testcase_24 AC 662 ms
111,380 KB
testcase_25 TLE -
testcase_26 AC 145 ms
80,128 KB
testcase_27 AC 323 ms
87,376 KB
testcase_28 AC 1,193 ms
145,552 KB
testcase_29 AC 1,159 ms
142,484 KB
testcase_30 AC 208 ms
83,276 KB
testcase_31 TLE -
testcase_32 AC 801 ms
116,216 KB
testcase_33 TLE -
testcase_34 AC 1,180 ms
142,108 KB
testcase_35 AC 681 ms
109,604 KB
testcase_36 AC 702 ms
111,836 KB
testcase_37 AC 988 ms
129,604 KB
testcase_38 AC 611 ms
107,716 KB
testcase_39 AC 414 ms
94,476 KB
testcase_40 AC 1,450 ms
162,744 KB
testcase_41 AC 1,971 ms
187,836 KB
testcase_42 AC 64 ms
67,200 KB
testcase_43 AC 68 ms
67,968 KB
testcase_44 AC 76 ms
67,840 KB
testcase_45 AC 66 ms
67,712 KB
testcase_46 AC 67 ms
68,608 KB
testcase_47 AC 67 ms
66,944 KB
testcase_48 AC 68 ms
67,328 KB
testcase_49 AC 67 ms
67,712 KB
testcase_50 AC 67 ms
67,840 KB
testcase_51 AC 76 ms
67,456 KB
testcase_52 AC 67 ms
67,456 KB
testcase_53 AC 68 ms
67,712 KB
testcase_54 AC 66 ms
67,584 KB
testcase_55 AC 66 ms
67,968 KB
testcase_56 AC 64 ms
67,200 KB
testcase_57 AC 64 ms
67,328 KB
testcase_58 AC 68 ms
68,096 KB
testcase_59 AC 67 ms
67,968 KB
testcase_60 AC 64 ms
67,200 KB
testcase_61 AC 65 ms
67,328 KB
testcase_62 AC 65 ms
66,944 KB
testcase_63 AC 65 ms
66,816 KB
testcase_64 AC 65 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:
        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