結果
| 問題 |
No.1051 PQ Permutation
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2024-08-15 03:47:40 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 341 ms / 2,000 ms |
| コード長 | 1,856 bytes |
| コンパイル時間 | 300 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 29,808 KB |
| 最終ジャッジ日時 | 2024-08-15 03:47:48 |
| 合計ジャッジ時間 | 8,351 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
def Next_Permutation(perm,inplace=False):
N=len(perm)
b=N-1
while b and perm[b-1]>perm[b]:
b-=1
if b==0:
return None
i=max(i for i in range(b,N) if perm[i]>perm[b-1])
if inplace:
queue=perm[i:i+1]+perm[:i:-1]+perm[b-1:b]+perm[i-1:b-1:-1]
while b-1<len(perm):
perm.pop()
perm+=queue
return perm
else:
i=max(i for i in range(b,N) if perm[i]>perm[b-1])
return perm[:b-1]+perm[i:i+1]+perm[:i:-1]+perm[b-1:b]+perm[i-1:b-1:-1]
N,P,Q=map(int,input().split())
A=list(map(int,input().split()))
if A==sorted(A,reverse=True):
print(-1)
exit()
A=Next_Permutation(A)
if A.index(P)<A.index(Q):
B=A
else:
if Q<P or max(A[A.index(Q):])!=Q:
i=A.index(Q)
B=A[:i]
BB=A[i:]
BB.sort()
j=BB.index(Q)
if BB[j+1]==P:
B.append(BB[j+1])
for b in BB:
if b==P:
continue
B.append(b)
else:
B.append(BB[j+1])
BB=BB[:j+1]+BB[j+2:]
for b in BB:
if b==Q and P>Q:
continue
B.append(b)
if b==P and P>Q:
B.append(Q)
else:
if all(A[i]>A[i+1] for i in range(A.index(Q))):
print(-1)
exit()
inf=1<<30
dp=A[:]
dp[A.index(Q)]=-inf
for i in range(N-2,-1,-1):
dp[i]=max(dp[i],dp[i+1])
idx=[i for i in range(A.index(Q)) if dp[i]>A[i]]
if not idx:
print(-1)
exit()
i=max(idx)
B=A[:i]
BB=A[i:]
BB.sort()
b=min(b for b in BB if b>A[i] and b!=Q)
B.append(b)
for bb in BB:
if b==bb:
continue
B.append(bb)
print(*B)
vwxyz