結果

問題 No.1051 PQ Permutation
ユーザー roarisroaris
提出日時 2020-05-15 02:33:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,078 bytes
コンパイル時間 1,109 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 165,968 KB
最終ジャッジ日時 2023-10-14 22:32:26
合計ジャッジ時間 13,067 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,408 KB
testcase_01 WA -
testcase_02 AC 78 ms
71,228 KB
testcase_03 AC 77 ms
71,504 KB
testcase_04 AC 76 ms
71,140 KB
testcase_05 AC 75 ms
71,512 KB
testcase_06 AC 77 ms
71,104 KB
testcase_07 AC 76 ms
71,228 KB
testcase_08 AC 77 ms
71,484 KB
testcase_09 AC 74 ms
71,384 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 75 ms
71,400 KB
testcase_13 AC 76 ms
71,356 KB
testcase_14 AC 76 ms
71,232 KB
testcase_15 AC 218 ms
120,772 KB
testcase_16 AC 217 ms
120,916 KB
testcase_17 AC 234 ms
165,968 KB
testcase_18 AC 232 ms
165,820 KB
testcase_19 AC 230 ms
165,592 KB
testcase_20 WA -
testcase_21 AC 218 ms
135,572 KB
testcase_22 AC 225 ms
165,760 KB
testcase_23 AC 221 ms
138,416 KB
testcase_24 AC 218 ms
137,108 KB
testcase_25 AC 101 ms
79,224 KB
testcase_26 AC 99 ms
79,068 KB
testcase_27 WA -
testcase_28 AC 112 ms
88,876 KB
testcase_29 AC 103 ms
79,040 KB
testcase_30 AC 105 ms
79,088 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 93 ms
76,904 KB
testcase_34 AC 94 ms
76,784 KB
testcase_35 AC 134 ms
108,372 KB
testcase_36 AC 137 ms
111,120 KB
testcase_37 AC 148 ms
113,660 KB
testcase_38 AC 228 ms
161,888 KB
testcase_39 AC 136 ms
109,104 KB
testcase_40 AC 218 ms
155,368 KB
testcase_41 AC 218 ms
155,440 KB
testcase_42 AC 75 ms
71,312 KB
testcase_43 AC 74 ms
71,092 KB
testcase_44 AC 75 ms
71,512 KB
testcase_45 AC 77 ms
71,332 KB
testcase_46 AC 77 ms
71,328 KB
testcase_47 AC 75 ms
71,412 KB
testcase_48 AC 153 ms
113,536 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 (Q in s) and (P not in s):
        break
    
    if (P not in s) and (Q not in s):
        if max1[i]!=Q and A[i]<max1[i]:
            match = i
        elif A[i]<max2[i]:
            match = i
    else:
        if A[i]<max1[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])

rem.sort()
s = set(ans)

if P in s:
    rem2 = []
    
    for i in range(len(rem)):
        if rem[i]==A[match]:
            mark = i+1
            
            for j in range(len(rem)):
                if j!=mark:
                    rem2.append(rem[j])
                    
            break
    
    ans.append(rem[mark])
    ans += rem2
else:
    rem2 = []
    
    for i in range(len(rem)):
        if rem[i]==A[match]:
            if rem[i+1]!=Q:
                mark = i+1
            else:
                mark = i+2
            
            for j in range(len(rem)):
                if j!=mark:
                    rem2.append(rem[j])
            
            break
    
    if P<Q:
        ans.append(rem[mark])
        ans += rem2
    else:
        if rem[mark]==P:
            ans.append(rem[mark])
            ans += rem2
        else:
            ans.append(A[mark])
        
            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)
    
print(*ans)
0