結果

問題 No.2740 Old Maid
ユーザー 👑 rin204rin204
提出日時 2024-04-20 13:15:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,430 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 82,692 KB
実行使用メモリ 112,964 KB
最終ジャッジ日時 2024-04-20 13:15:41
合計ジャッジ時間 13,687 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 216 ms
112,612 KB
testcase_01 AC 217 ms
112,736 KB
testcase_02 AC 239 ms
112,528 KB
testcase_03 AC 251 ms
112,512 KB
testcase_04 AC 223 ms
112,640 KB
testcase_05 AC 227 ms
112,640 KB
testcase_06 AC 224 ms
112,384 KB
testcase_07 AC 224 ms
112,768 KB
testcase_08 AC 250 ms
112,512 KB
testcase_09 AC 211 ms
112,512 KB
testcase_10 AC 214 ms
112,512 KB
testcase_11 AC 203 ms
112,964 KB
testcase_12 AC 242 ms
100,992 KB
testcase_13 AC 257 ms
104,576 KB
testcase_14 AC 158 ms
81,280 KB
testcase_15 AC 183 ms
82,944 KB
testcase_16 AC 217 ms
94,976 KB
testcase_17 AC 163 ms
80,952 KB
testcase_18 AC 264 ms
101,316 KB
testcase_19 AC 185 ms
86,204 KB
testcase_20 AC 307 ms
107,904 KB
testcase_21 AC 159 ms
81,792 KB
testcase_22 AC 215 ms
95,008 KB
testcase_23 AC 145 ms
79,412 KB
testcase_24 AC 172 ms
85,504 KB
testcase_25 AC 306 ms
108,544 KB
testcase_26 AC 94 ms
76,544 KB
testcase_27 AC 139 ms
77,944 KB
testcase_28 AC 217 ms
95,104 KB
testcase_29 AC 219 ms
93,440 KB
testcase_30 AC 116 ms
77,576 KB
testcase_31 AC 302 ms
112,128 KB
testcase_32 AC 195 ms
87,552 KB
testcase_33 AC 316 ms
108,800 KB
testcase_34 AC 211 ms
92,928 KB
testcase_35 AC 174 ms
84,940 KB
testcase_36 AC 178 ms
85,620 KB
testcase_37 AC 195 ms
90,752 KB
testcase_38 AC 180 ms
84,788 KB
testcase_39 AC 161 ms
79,584 KB
testcase_40 AC 234 ms
98,432 KB
testcase_41 AC 301 ms
107,264 KB
testcase_42 AC 42 ms
52,608 KB
testcase_43 AC 42 ms
52,608 KB
testcase_44 AC 45 ms
52,768 KB
testcase_45 AC 46 ms
52,608 KB
testcase_46 AC 42 ms
52,480 KB
testcase_47 AC 41 ms
52,480 KB
testcase_48 AC 41 ms
52,352 KB
testcase_49 AC 41 ms
52,224 KB
testcase_50 AC 42 ms
52,224 KB
testcase_51 AC 41 ms
52,864 KB
testcase_52 AC 42 ms
52,864 KB
testcase_53 AC 42 ms
52,352 KB
testcase_54 AC 43 ms
52,608 KB
testcase_55 AC 43 ms
52,352 KB
testcase_56 AC 41 ms
52,736 KB
testcase_57 AC 42 ms
52,352 KB
testcase_58 AC 43 ms
52,480 KB
testcase_59 AC 42 ms
52,352 KB
testcase_60 AC 42 ms
52,352 KB
testcase_61 AC 42 ms
52,480 KB
testcase_62 AC 41 ms
52,224 KB
testcase_63 AC 42 ms
52,480 KB
testcase_64 AC 42 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [-1] * n
        self.group_ = n
        self.R = [i for i in range(n)]

    def find(self, x):
        if self.par[x] < 0:
            return x
        lst = []
        while self.par[x] >= 0:
            lst.append(x)
            x = self.par[x]
        for y in lst:
            self.par[y] = x
        return x

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False

        if self.par[x] > self.par[y]:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        self.R[x] = max(self.R[x], self.R[y])
        self.group_ -= 1
        return True

    def size(self, x):
        return -self.par[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    @property
    def group(self):
        return self.group_


n = int(input())
P = list(map(int, input().split()))
for i in range(n):
    P[i] -= 1

invP = [0] * n
for i in range(n):
    invP[P[i]] = i

Q = []

p = 0
UF = UnionFind(n)
used = [False] * n
while p < n:
    if used[p]:
        p += 1
        continue

    x = invP[p]
    if UF.R[UF.find(x)] == n - 1:
        p += 1
        continue

    y = UF.R[UF.find(x)] + 1
    q = P[y]
    Q += [p + 1, q + 1]
    UF.unite(x, x - 1)
    UF.unite(y, y - 1)
    used[p] = True
    used[q] = True

print(*Q)
0