結果

問題 No.1051 PQ Permutation
ユーザー roarisroaris
提出日時 2020-05-15 02:35:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 2,080 bytes
コンパイル時間 2,692 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 165,784 KB
最終ジャッジ日時 2023-10-14 22:36:18
合計ジャッジ時間 11,741 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,060 KB
testcase_01 AC 68 ms
70,960 KB
testcase_02 AC 69 ms
70,812 KB
testcase_03 AC 67 ms
70,824 KB
testcase_04 AC 66 ms
71,052 KB
testcase_05 AC 66 ms
71,068 KB
testcase_06 AC 64 ms
71,044 KB
testcase_07 AC 66 ms
70,888 KB
testcase_08 AC 63 ms
70,816 KB
testcase_09 AC 63 ms
71,280 KB
testcase_10 AC 65 ms
71,056 KB
testcase_11 AC 67 ms
71,284 KB
testcase_12 AC 67 ms
70,996 KB
testcase_13 AC 67 ms
71,340 KB
testcase_14 AC 67 ms
71,000 KB
testcase_15 AC 220 ms
120,708 KB
testcase_16 AC 199 ms
120,820 KB
testcase_17 AC 224 ms
165,784 KB
testcase_18 AC 219 ms
165,636 KB
testcase_19 AC 222 ms
165,636 KB
testcase_20 AC 219 ms
145,732 KB
testcase_21 AC 208 ms
135,324 KB
testcase_22 AC 221 ms
165,284 KB
testcase_23 AC 216 ms
138,576 KB
testcase_24 AC 215 ms
137,144 KB
testcase_25 AC 92 ms
79,280 KB
testcase_26 AC 95 ms
78,888 KB
testcase_27 AC 86 ms
76,732 KB
testcase_28 AC 104 ms
88,732 KB
testcase_29 AC 92 ms
78,700 KB
testcase_30 AC 96 ms
79,220 KB
testcase_31 AC 94 ms
77,200 KB
testcase_32 AC 99 ms
78,080 KB
testcase_33 AC 85 ms
76,804 KB
testcase_34 AC 86 ms
76,628 KB
testcase_35 AC 133 ms
108,200 KB
testcase_36 AC 129 ms
110,932 KB
testcase_37 AC 139 ms
113,584 KB
testcase_38 AC 221 ms
161,412 KB
testcase_39 AC 131 ms
108,832 KB
testcase_40 AC 212 ms
155,000 KB
testcase_41 AC 207 ms
155,300 KB
testcase_42 AC 77 ms
71,044 KB
testcase_43 AC 77 ms
70,940 KB
testcase_44 AC 69 ms
71,084 KB
testcase_45 AC 70 ms
70,892 KB
testcase_46 AC 68 ms
71,316 KB
testcase_47 AC 69 ms
71,272 KB
testcase_48 AC 141 ms
113,444 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(rem[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