結果

問題 No.1051 PQ Permutation
ユーザー maspymaspy
提出日時 2020-05-09 12:18:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 914 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 10,864 KB
実行使用メモリ 366,624 KB
最終ジャッジ日時 2023-09-19 08:20:17
合計ジャッジ時間 11,585 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,444 KB
testcase_01 AC 17 ms
8,240 KB
testcase_02 AC 17 ms
8,204 KB
testcase_03 AC 17 ms
8,164 KB
testcase_04 WA -
testcase_05 AC 17 ms
8,240 KB
testcase_06 WA -
testcase_07 AC 18 ms
8,272 KB
testcase_08 AC 17 ms
8,204 KB
testcase_09 AC 18 ms
8,204 KB
testcase_10 AC 17 ms
8,276 KB
testcase_11 AC 18 ms
8,244 KB
testcase_12 WA -
testcase_13 AC 18 ms
8,236 KB
testcase_14 WA -
testcase_15 AC 442 ms
26,816 KB
testcase_16 AC 415 ms
26,952 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 322 ms
26,796 KB
testcase_21 AC 332 ms
26,836 KB
testcase_22 WA -
testcase_23 AC 319 ms
26,968 KB
testcase_24 AC 315 ms
26,872 KB
testcase_25 AC 36 ms
9,952 KB
testcase_26 WA -
testcase_27 AC 20 ms
8,584 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 38 ms
9,876 KB
testcase_31 AC 22 ms
8,644 KB
testcase_32 AC 37 ms
9,968 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 136 ms
18,212 KB
testcase_36 AC 124 ms
18,244 KB
testcase_37 AC 102 ms
26,804 KB
testcase_38 AC 266 ms
26,900 KB
testcase_39 AC 153 ms
26,872 KB
testcase_40 WA -
testcase_41 AC 215 ms
26,892 KB
testcase_42 AC 17 ms
8,232 KB
testcase_43 AC 17 ms
8,332 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 18 ms
8,336 KB
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

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
    if P > Q:
        del A[q]
        A.insert(p, Q)
        return A
    return solve(A, P, Q)

solve(A, P, Q)

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

0