結果

問題 No.1051 PQ Permutation
ユーザー roarisroaris
提出日時 2020-05-11 18:34:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,803 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 86,776 KB
実行使用メモリ 165,836 KB
最終ジャッジ日時 2023-09-25 21:47:06
合計ジャッジ時間 10,320 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,192 KB
testcase_01 AC 71 ms
71,196 KB
testcase_02 AC 74 ms
71,052 KB
testcase_03 AC 74 ms
70,868 KB
testcase_04 AC 73 ms
70,864 KB
testcase_05 AC 72 ms
71,328 KB
testcase_06 AC 73 ms
70,976 KB
testcase_07 AC 71 ms
70,868 KB
testcase_08 AC 72 ms
71,004 KB
testcase_09 AC 71 ms
71,040 KB
testcase_10 AC 73 ms
71,328 KB
testcase_11 AC 74 ms
71,056 KB
testcase_12 AC 74 ms
71,108 KB
testcase_13 AC 74 ms
71,040 KB
testcase_14 AC 74 ms
70,920 KB
testcase_15 AC 224 ms
120,740 KB
testcase_16 AC 214 ms
120,868 KB
testcase_17 AC 227 ms
165,836 KB
testcase_18 AC 230 ms
165,572 KB
testcase_19 AC 224 ms
165,672 KB
testcase_20 AC 232 ms
145,628 KB
testcase_21 AC 220 ms
135,400 KB
testcase_22 AC 223 ms
165,804 KB
testcase_23 AC 218 ms
138,308 KB
testcase_24 AC 219 ms
137,012 KB
testcase_25 AC 102 ms
78,968 KB
testcase_26 AC 92 ms
77,628 KB
testcase_27 AC 90 ms
76,656 KB
testcase_28 AC 107 ms
89,148 KB
testcase_29 AC 92 ms
77,568 KB
testcase_30 AC 100 ms
79,048 KB
testcase_31 AC 97 ms
77,328 KB
testcase_32 AC 107 ms
78,288 KB
testcase_33 AC 90 ms
76,628 KB
testcase_34 AC 90 ms
76,636 KB
testcase_35 AC 132 ms
108,284 KB
testcase_36 AC 137 ms
110,628 KB
testcase_37 AC 146 ms
113,484 KB
testcase_38 AC 224 ms
161,640 KB
testcase_39 AC 135 ms
109,056 KB
testcase_40 AC 217 ms
155,272 KB
testcase_41 AC 216 ms
155,268 KB
testcase_42 AC 72 ms
71,256 KB
testcase_43 AC 70 ms
71,288 KB
testcase_44 AC 70 ms
71,020 KB
testcase_45 AC 70 ms
71,356 KB
testcase_46 AC 73 ms
71,112 KB
testcase_47 AC 71 ms
70,916 KB
testcase_48 AC 146 ms
113,412 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:
        if rem[mark]==P:
            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