結果

問題 No.1051 PQ Permutation
ユーザー 👑 rin204rin204
提出日時 2023-12-29 15:58:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,176 bytes
コンパイル時間 698 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 111,732 KB
最終ジャッジ日時 2023-12-29 15:58:14
合計ジャッジ時間 8,111 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 WA -
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 35 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 35 ms
53,460 KB
testcase_09 AC 35 ms
53,460 KB
testcase_10 WA -
testcase_11 AC 36 ms
53,460 KB
testcase_12 AC 35 ms
53,460 KB
testcase_13 RE -
testcase_14 AC 36 ms
53,460 KB
testcase_15 AC 161 ms
107,636 KB
testcase_16 AC 159 ms
107,764 KB
testcase_17 AC 125 ms
108,916 KB
testcase_18 AC 119 ms
108,916 KB
testcase_19 AC 119 ms
108,916 KB
testcase_20 AC 150 ms
108,404 KB
testcase_21 AC 148 ms
107,508 KB
testcase_22 AC 120 ms
108,916 KB
testcase_23 AC 146 ms
108,148 KB
testcase_24 AC 157 ms
107,508 KB
testcase_25 AC 58 ms
73,232 KB
testcase_26 AC 54 ms
68,684 KB
testcase_27 AC 51 ms
64,560 KB
testcase_28 AC 57 ms
76,036 KB
testcase_29 AC 50 ms
68,712 KB
testcase_30 AC 59 ms
73,228 KB
testcase_31 AC 53 ms
66,652 KB
testcase_32 AC 59 ms
72,840 KB
testcase_33 AC 47 ms
64,548 KB
testcase_34 AC 48 ms
64,548 KB
testcase_35 RE -
testcase_36 AC 91 ms
100,260 KB
testcase_37 AC 79 ms
102,644 KB
testcase_38 AC 138 ms
107,764 KB
testcase_39 AC 85 ms
104,308 KB
testcase_40 AC 118 ms
108,276 KB
testcase_41 AC 124 ms
111,732 KB
testcase_42 RE -
testcase_43 RE -
testcase_44 AC 35 ms
53,460 KB
testcase_45 AC 35 ms
53,460 KB
testcase_46 AC 36 ms
53,460 KB
testcase_47 RE -
testcase_48 RE -
権限があれば一括ダウンロードができます

ソースコード

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()
    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:
            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 > q and b < mi:
            mi = b

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