結果

問題 No.1051 PQ Permutation
ユーザー 👑 rin204rin204
提出日時 2023-12-29 16:04:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 2,274 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 111,604 KB
最終ジャッジ日時 2023-12-29 16:04:08
合計ジャッジ時間 7,374 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 35 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 35 ms
53,460 KB
testcase_08 AC 36 ms
53,460 KB
testcase_09 AC 35 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 35 ms
53,460 KB
testcase_12 AC 36 ms
53,460 KB
testcase_13 AC 36 ms
53,460 KB
testcase_14 AC 36 ms
53,460 KB
testcase_15 AC 160 ms
107,636 KB
testcase_16 AC 160 ms
107,636 KB
testcase_17 AC 117 ms
108,788 KB
testcase_18 AC 117 ms
108,788 KB
testcase_19 AC 118 ms
108,788 KB
testcase_20 AC 149 ms
108,404 KB
testcase_21 AC 147 ms
107,636 KB
testcase_22 AC 117 ms
108,788 KB
testcase_23 AC 143 ms
107,636 KB
testcase_24 AC 146 ms
107,636 KB
testcase_25 AC 59 ms
73,236 KB
testcase_26 AC 51 ms
68,684 KB
testcase_27 AC 50 ms
64,560 KB
testcase_28 AC 56 ms
76,036 KB
testcase_29 AC 52 ms
68,712 KB
testcase_30 AC 58 ms
73,232 KB
testcase_31 AC 54 ms
66,652 KB
testcase_32 AC 59 ms
72,840 KB
testcase_33 AC 48 ms
64,548 KB
testcase_34 AC 48 ms
64,548 KB
testcase_35 AC 89 ms
94,960 KB
testcase_36 AC 90 ms
100,260 KB
testcase_37 AC 78 ms
102,644 KB
testcase_38 AC 133 ms
107,764 KB
testcase_39 AC 85 ms
104,308 KB
testcase_40 AC 124 ms
108,276 KB
testcase_41 AC 122 ms
111,604 KB
testcase_42 AC 36 ms
53,460 KB
testcase_43 AC 36 ms
53,460 KB
testcase_44 AC 36 ms
53,460 KB
testcase_45 AC 35 ms
53,460 KB
testcase_46 AC 37 ms
53,460 KB
testcase_47 AC 36 ms
53,460 KB
testcase_48 AC 80 ms
103,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def next_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] < P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] < P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def all_permutations(P):
    # 全列挙したい場合はソートしてある状態で渡す
    yield P
    while next_permutation(P):
        yield P


def prev_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] > P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] > P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def rev_all_permutations(P):
    # 全列挙したい場合は逆順ソートしてある状態で渡す
    yield P
    while prev_permutation(P):
        yield P


n, p, q = map(int, input().split())
A = list(map(int, input().split()))
if not next_permutation(A):
    print(-1)
    exit()


pp = -1
qq = -1
for i, a in enumerate(A):
    if a == p:
        pp = i
    if a == q:
        qq = i

if pp < qq:
    print(*A)
    exit()

if q < p:
    B = A[qq:]
    A = A[:qq]
    mi = 1 << 30
    for b in B:
        if b > q and b < mi:
            mi = b

    A.append(mi)
    B.remove(mi)
    B.sort()
    if mi == p:
        A += B
    else:
        for b in B:
            if b == q:
                pass
            elif b == p:
                A += [p, q]
            else:
                A.append(b)

    print(*A)
else:
    ma = -1
    ind = -1
    for i in range(n - 1, -1, -1):
        if A[i] > ma and A[i] != q:
            ma = A[i]
        if i <= qq and ma > A[i]:
            ind = i
            break

    if ind == -1:
        print(-1)
        exit()

    B = A[ind:]
    A = A[:ind]

    mi = 1 << 30
    for b in B:
        if b > B[0] and b != q and b < mi:
            mi = b

    A.append(mi)
    B.remove(mi)
    B.sort()
    A += B
    print(*A)
0