結果
| 問題 |
No.270 next_permutation (1)
|
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2020-04-01 01:49:03 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 511 ms / 2,000 ms |
| コード長 | 956 bytes |
| コンパイル時間 | 95 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 24,216 KB |
| 最終ジャッジ日時 | 2024-06-25 17:02:05 |
| 合計ジャッジ時間 | 4,335 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect_left
import itertools
N, K = map(int, readline().split())
P = [0] + list(map(int, readline().split()))
B = [0] + list(map(int, readline().split()))
if K == 0:
print(0)
exit()
D = [sum(abs(x - y) for x, y in zip(P, B))]
for _ in range(K - 1):
diff = 0
Q = []
while P[-2] > P[-1]:
x = P.pop()
diff -= abs(x - B[len(P)])
Q.append(x)
x = P.pop()
diff -= abs(x - B[len(P)])
Q.append(x)
if not P[-1]:
P = list(range(N + 1))
D.append(sum(abs(x - y) for x, y in zip(P, B)))
continue
p = P.pop()
diff -= abs(p - B[len(P)])
n = bisect_left(Q, p)
for x in itertools.chain([Q[n]], Q[:n], [p], Q[n + 1:]):
diff += abs(x - B[len(P)])
P.append(x)
D.append(D[-1] + diff)
print(sum(D))
maspy