結果

問題 No.270 next_permutation (1)
ユーザー maspymaspy
提出日時 2020-04-01 01:49:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 956 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 10,704 KB
実行使用メモリ 21,268 KB
最終ジャッジ日時 2023-09-07 23:35:40
合計ジャッジ時間 4,352 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
8,856 KB
testcase_01 AC 222 ms
10,872 KB
testcase_02 AC 254 ms
10,852 KB
testcase_03 AC 17 ms
8,252 KB
testcase_04 AC 196 ms
10,212 KB
testcase_05 AC 207 ms
10,364 KB
testcase_06 AC 209 ms
10,368 KB
testcase_07 AC 79 ms
9,148 KB
testcase_08 AC 80 ms
9,088 KB
testcase_09 AC 206 ms
11,708 KB
testcase_10 AC 264 ms
21,252 KB
testcase_11 AC 462 ms
21,228 KB
testcase_12 AC 468 ms
21,268 KB
testcase_13 AC 200 ms
20,916 KB
testcase_14 AC 245 ms
20,808 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()))

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