結果

問題 No.1051 PQ Permutation
ユーザー maspymaspy
提出日時 2020-05-14 18:58:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,018 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 11,000 KB
実行使用メモリ 26,920 KB
最終ジャッジ日時 2023-10-13 17:25:38
合計ジャッジ時間 7,257 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,304 KB
testcase_01 AC 16 ms
8,200 KB
testcase_02 AC 17 ms
8,248 KB
testcase_03 AC 17 ms
8,264 KB
testcase_04 AC 17 ms
8,272 KB
testcase_05 AC 17 ms
8,320 KB
testcase_06 AC 16 ms
8,260 KB
testcase_07 AC 16 ms
8,324 KB
testcase_08 AC 17 ms
8,324 KB
testcase_09 AC 16 ms
8,320 KB
testcase_10 AC 16 ms
8,252 KB
testcase_11 AC 17 ms
8,252 KB
testcase_12 AC 17 ms
8,296 KB
testcase_13 AC 17 ms
8,188 KB
testcase_14 AC 16 ms
8,248 KB
testcase_15 AC 289 ms
26,824 KB
testcase_16 AC 275 ms
26,844 KB
testcase_17 AC 157 ms
26,900 KB
testcase_18 AC 157 ms
26,788 KB
testcase_19 AC 156 ms
26,820 KB
testcase_20 AC 233 ms
26,848 KB
testcase_21 AC 239 ms
26,792 KB
testcase_22 AC 157 ms
26,792 KB
testcase_23 AC 231 ms
26,848 KB
testcase_24 AC 231 ms
26,904 KB
testcase_25 AC 31 ms
10,100 KB
testcase_26 AC 24 ms
9,236 KB
testcase_27 AC 19 ms
8,464 KB
testcase_28 AC 36 ms
11,188 KB
testcase_29 AC 23 ms
9,252 KB
testcase_30 AC 33 ms
10,056 KB
testcase_31 AC 21 ms
8,632 KB
testcase_32 AC 33 ms
9,816 KB
testcase_33 AC 19 ms
8,672 KB
testcase_34 AC 18 ms
8,648 KB
testcase_35 AC 121 ms
18,168 KB
testcase_36 AC 105 ms
18,028 KB
testcase_37 AC 97 ms
26,920 KB
testcase_38 AC 224 ms
26,792 KB
testcase_39 AC 123 ms
26,912 KB
testcase_40 AC 168 ms
26,824 KB
testcase_41 AC 190 ms
26,920 KB
testcase_42 WA -
testcase_43 AC 17 ms
8,268 KB
testcase_44 AC 17 ms
8,188 KB
testcase_45 AC 17 ms
8,192 KB
testcase_46 AC 16 ms
8,280 KB
testcase_47 AC 16 ms
8,252 KB
testcase_48 AC 128 ms
26,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, P, Q = map(int, readline().split())
A = [0] + list(map(int, read().split()))

def next_perm(A):
    B = [A.pop()]
    while A[-1] > B[-1]:
        B.append(A.pop())
    x = A.pop()
    i = bisect.bisect_left(B, x)
    A.append(B[i])
    B[i] = x
    A += B
    return A

def solve(A, P, Q):
    next_perm(A)
    if A[0]:
        return False
    p = A.index(P)
    q = A.index(Q)
    if p < q:
        return A
    if max(A[q:]) == Q:
        second = max(A[q + 1:])
        q -= 1
        while A[q] > second:
            q -= 1
    # とりあえず、Qを動かす
    A = A[:q + 1] + sorted(A[q + 1:], reverse=True)
    next_perm(A)
    p = A.index(P)
    q = A.index(Q)
    if p < q:
        return A
    if P > Q:
        del A[q]
        A.insert(p, Q)
        return A
    return solve(A, P, Q)

A = solve(A, P, Q)
if not A or A[0] != 0:
    print(-1)
else:
    print(*A[1:])
0