結果

問題 No.270 next_permutation (1)
ユーザー maspymaspy
提出日時 2020-04-01 01:48:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 915 bytes
コンパイル時間 84 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 21,312 KB
最終ジャッジ日時 2023-09-07 23:34:34
合計ジャッジ時間 4,685 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
9,176 KB
testcase_01 AC 227 ms
10,884 KB
testcase_02 AC 257 ms
10,976 KB
testcase_03 WA -
testcase_04 AC 198 ms
10,144 KB
testcase_05 AC 195 ms
10,260 KB
testcase_06 AC 209 ms
10,348 KB
testcase_07 AC 74 ms
9,196 KB
testcase_08 AC 77 ms
9,244 KB
testcase_09 AC 200 ms
11,524 KB
testcase_10 AC 256 ms
21,312 KB
testcase_11 AC 456 ms
21,256 KB
testcase_12 AC 452 ms
21,304 KB
testcase_13 AC 191 ms
21,000 KB
testcase_14 AC 247 ms
20,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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()))

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))
0