結果

問題 No.1051 PQ Permutation
ユーザー maspymaspy
提出日時 2020-05-08 21:57:02
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 861 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 10,968 KB
実行使用メモリ 26,960 KB
最終ジャッジ日時 2023-09-19 07:35:38
合計ジャッジ時間 7,840 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,336 KB
testcase_01 AC 17 ms
8,192 KB
testcase_02 AC 17 ms
8,308 KB
testcase_03 AC 17 ms
8,244 KB
testcase_04 AC 16 ms
8,212 KB
testcase_05 AC 17 ms
8,188 KB
testcase_06 AC 16 ms
8,196 KB
testcase_07 AC 16 ms
8,204 KB
testcase_08 AC 16 ms
8,252 KB
testcase_09 AC 17 ms
8,260 KB
testcase_10 AC 16 ms
8,188 KB
testcase_11 AC 16 ms
8,188 KB
testcase_12 AC 17 ms
8,312 KB
testcase_13 AC 16 ms
8,216 KB
testcase_14 AC 17 ms
8,364 KB
testcase_15 AC 298 ms
26,816 KB
testcase_16 AC 278 ms
26,912 KB
testcase_17 AC 157 ms
26,860 KB
testcase_18 AC 157 ms
26,928 KB
testcase_19 AC 159 ms
26,960 KB
testcase_20 AC 242 ms
26,948 KB
testcase_21 AC 252 ms
26,804 KB
testcase_22 AC 158 ms
26,908 KB
testcase_23 AC 246 ms
26,808 KB
testcase_24 AC 234 ms
26,852 KB
testcase_25 AC 31 ms
10,212 KB
testcase_26 AC 24 ms
9,256 KB
testcase_27 AC 19 ms
8,416 KB
testcase_28 AC 37 ms
11,144 KB
testcase_29 AC 24 ms
9,344 KB
testcase_30 AC 34 ms
10,108 KB
testcase_31 AC 21 ms
8,764 KB
testcase_32 AC 33 ms
9,964 KB
testcase_33 AC 19 ms
8,644 KB
testcase_34 AC 19 ms
8,632 KB
testcase_35 AC 118 ms
18,200 KB
testcase_36 AC 104 ms
18,204 KB
testcase_37 AC 97 ms
26,924 KB
testcase_38 AC 221 ms
26,792 KB
testcase_39 AC 109 ms
26,856 KB
testcase_40 AC 167 ms
26,852 KB
testcase_41 AC 195 ms
26,952 KB
testcase_42 RE -
testcase_43 AC 17 ms
8,112 KB
testcase_44 AC 17 ms
8,360 KB
testcase_45 AC 17 ms
8,216 KB
testcase_46 AC 17 ms
8,364 KB
testcase_47 RE -
testcase_48 RE -
権限があれば一括ダウンロードができます

ソースコード

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
    # とりあえず、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
    assert P > Q
    del A[q]
    A.insert(p, Q)
    return A

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