結果

問題 No.1051 PQ Permutation
ユーザー roarisroaris
提出日時 2020-05-11 18:24:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,712 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 165,492 KB
最終ジャッジ日時 2023-09-25 21:14:18
合計ジャッジ時間 11,878 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
70,924 KB
testcase_01 AC 69 ms
70,924 KB
testcase_02 AC 69 ms
71,044 KB
testcase_03 WA -
testcase_04 AC 69 ms
71,132 KB
testcase_05 AC 70 ms
70,936 KB
testcase_06 WA -
testcase_07 AC 70 ms
71,004 KB
testcase_08 AC 73 ms
70,816 KB
testcase_09 AC 71 ms
71,132 KB
testcase_10 AC 71 ms
71,272 KB
testcase_11 AC 70 ms
71,012 KB
testcase_12 AC 70 ms
70,932 KB
testcase_13 AC 70 ms
71,120 KB
testcase_14 WA -
testcase_15 AC 212 ms
120,464 KB
testcase_16 AC 208 ms
120,548 KB
testcase_17 AC 219 ms
165,492 KB
testcase_18 AC 218 ms
165,296 KB
testcase_19 AC 218 ms
165,396 KB
testcase_20 AC 226 ms
145,340 KB
testcase_21 AC 213 ms
135,452 KB
testcase_22 AC 219 ms
165,360 KB
testcase_23 AC 211 ms
138,356 KB
testcase_24 AC 211 ms
136,956 KB
testcase_25 AC 96 ms
78,808 KB
testcase_26 AC 90 ms
77,400 KB
testcase_27 AC 89 ms
76,688 KB
testcase_28 AC 107 ms
88,940 KB
testcase_29 AC 90 ms
77,412 KB
testcase_30 AC 98 ms
79,004 KB
testcase_31 AC 92 ms
77,220 KB
testcase_32 AC 102 ms
77,960 KB
testcase_33 AC 84 ms
76,644 KB
testcase_34 AC 87 ms
76,484 KB
testcase_35 AC 124 ms
108,064 KB
testcase_36 AC 132 ms
110,668 KB
testcase_37 AC 139 ms
113,436 KB
testcase_38 AC 217 ms
161,336 KB
testcase_39 AC 128 ms
108,924 KB
testcase_40 WA -
testcase_41 AC 210 ms
155,048 KB
testcase_42 AC 70 ms
71,168 KB
testcase_43 AC 71 ms
71,172 KB
testcase_44 AC 70 ms
70,980 KB
testcase_45 AC 68 ms
71,036 KB
testcase_46 AC 69 ms
71,016 KB
testcase_47 AC 69 ms
71,012 KB
testcase_48 AC 142 ms
113,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N, P, Q = map(int, input().split())
A = list(map(int, input().split()))
max1 = [-1]*(N+1)
max2 = [-2]*(N+1)

for i in range(N-1, -1, -1):
    if A[i]>max1[i+1]:
        max1[i] = A[i]
        max2[i] = max1[i+1]
    elif A[i]>max2[i+1]:
        max1[i] = max1[i+1]
        max2[i] = A[i]
    else:
        max1[i] = max1[i+1]
        max2[i] = max2[i+1]
        
match = -1
s = set()

for i in range(N):
    if (P not in s) and (Q not in s):
        if max1[i]!=Q and max1[i]>A[i]:
            match = i
        elif max1[i]==Q and max2[i]>A[i]:
            match = i
    elif (P not in s) and (Q in s):
        break
    else:
        if max1[i]>A[i]:
            match = i

    s.add(A[i])

if match==-1:
    print(-1)
    exit()
    
ans = []
rem = []

for i in range(N):
    if i<match:
        ans.append(A[i])
    else:
        rem.append(A[i])

s = set(ans)
rem.sort()

if (P not in s) and (Q not in s):
    for i in range(len(rem)):
        if rem[i]==A[match]:
            if rem[i+1]!=Q:
                mark = i+1
            else:
                mark = i+2
    
    ans.append(rem[mark])
    rem2 = [rem[i] for i in range(len(rem)) if i!=mark]
    
    if P<Q:
        ans += rem2
    else:
        for rem2_i in rem2:
            if rem2_i<P and rem2_i!=Q:
                ans.append(rem2_i)
        
        ans.append(P)
        ans.append(Q)
        
        for rem2_i in rem2:
            if rem2_i>P:
                ans.append(rem2_i)
else:
    for i in range(len(rem)):
        if rem[i]==A[match]:
            mark = i+1
    
    ans.append(rem[mark])
    
    for i in range(len(rem)):
        if i!=mark:
            ans.append(rem[i])

print(*ans)
0