結果

問題 No.2740 Old Maid
ユーザー noriocnorioc
提出日時 2024-04-25 00:06:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 175,024 KB
最終ジャッジ日時 2024-04-25 00:06:45
合計ジャッジ時間 11,968 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 213 ms
174,008 KB
testcase_01 AC 215 ms
174,144 KB
testcase_02 AC 210 ms
174,264 KB
testcase_03 AC 213 ms
172,996 KB
testcase_04 AC 213 ms
171,064 KB
testcase_05 AC 224 ms
171,320 KB
testcase_06 AC 214 ms
174,008 KB
testcase_07 AC 220 ms
170,816 KB
testcase_08 AC 219 ms
174,032 KB
testcase_09 AC 215 ms
174,012 KB
testcase_10 AC 212 ms
174,900 KB
testcase_11 AC 208 ms
175,024 KB
testcase_12 AC 196 ms
141,044 KB
testcase_13 AC 210 ms
149,840 KB
testcase_14 AC 97 ms
91,136 KB
testcase_15 AC 102 ms
92,124 KB
testcase_16 AC 171 ms
122,488 KB
testcase_17 AC 92 ms
84,848 KB
testcase_18 AC 207 ms
142,772 KB
testcase_19 AC 122 ms
98,216 KB
testcase_20 AC 239 ms
161,736 KB
testcase_21 AC 106 ms
88,192 KB
testcase_22 AC 173 ms
122,144 KB
testcase_23 AC 88 ms
86,400 KB
testcase_24 AC 119 ms
98,108 KB
testcase_25 AC 242 ms
167,292 KB
testcase_26 AC 66 ms
68,864 KB
testcase_27 AC 82 ms
81,664 KB
testcase_28 AC 172 ms
122,512 KB
testcase_29 AC 189 ms
117,544 KB
testcase_30 AC 69 ms
72,704 KB
testcase_31 AC 252 ms
173,440 KB
testcase_32 AC 127 ms
100,984 KB
testcase_33 AC 236 ms
168,576 KB
testcase_34 AC 161 ms
116,332 KB
testcase_35 AC 131 ms
96,500 KB
testcase_36 AC 119 ms
98,160 KB
testcase_37 AC 148 ms
108,988 KB
testcase_38 AC 107 ms
95,208 KB
testcase_39 AC 91 ms
89,088 KB
testcase_40 AC 190 ms
132,180 KB
testcase_41 AC 228 ms
161,064 KB
testcase_42 AC 37 ms
52,352 KB
testcase_43 AC 37 ms
52,352 KB
testcase_44 AC 52 ms
52,480 KB
testcase_45 AC 37 ms
52,608 KB
testcase_46 AC 37 ms
52,352 KB
testcase_47 AC 37 ms
52,224 KB
testcase_48 AC 37 ms
52,224 KB
testcase_49 AC 37 ms
52,736 KB
testcase_50 AC 39 ms
52,608 KB
testcase_51 AC 37 ms
52,736 KB
testcase_52 AC 39 ms
52,224 KB
testcase_53 AC 38 ms
52,608 KB
testcase_54 AC 38 ms
52,096 KB
testcase_55 AC 36 ms
52,352 KB
testcase_56 AC 37 ms
52,480 KB
testcase_57 AC 38 ms
52,352 KB
testcase_58 AC 38 ms
52,608 KB
testcase_59 AC 38 ms
52,352 KB
testcase_60 AC 37 ms
52,480 KB
testcase_61 AC 37 ms
52,480 KB
testcase_62 AC 36 ms
52,352 KB
testcase_63 AC 37 ms
52,480 KB
testcase_64 AC 37 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None


class LinkedList:
    def __init__(self):
        self.front = None
        self.back = None
        self.size = 0

    # a の後ろに b をつなぐ
    def _join(self, a: Node, b: Node):
        a.next = b
        b.prev = a

    def append(self, val) -> Node:
        node = Node(val)
        if self.front is None:
            self.front = self.back = node
        else:
            self._join(self.back, node)
            self.back = node

        self.size += 1
        return node

    def appendleft(self, val) -> Node:
        node = Node(val)
        if self.front is None:
            self.front = self.back = node
        else:
            self._join(node, self.front)
            self.front = node

        self.size += 1
        return node

    def remove(self, node: Node):
        assert self.size > 0

        if self.size == 1:
            self.front = self.back = None
        elif node is self.front:
            self.front = self.front.next
            self.front.prev = None
        elif node is self.back:
            self.back = self.back.prev
            self.back.next = None
        else:
            prev = node.prev
            next = node.next
            prev.next = next
            next.prev = prev

        self.size -= 1

    def __len__(self):
        return self.size

    def __iter__(self):
        if self.front is None: return
        node = self.front
        while node is not None:
            yield node.val
            node = node.next


N = int(input())
P = list(map(lambda x: int(x)-1, input().split()))

ll = LinkedList()
p2node = {}
for p in P:
    p2node[p] = ll.append(p)

used = set()
ans = []
for i in range(N):
    if i in used: continue
    node = p2node[i]
    next = node.next
    if next is None: continue  # 2 つの要素が連蔵していない
    ans.append(node.val + 1)
    ans.append(next.val + 1)
    ll.remove(node)
    ll.remove(next)
    used.add(node.val)
    used.add(next.val)

print(*ans)
0