結果

問題 No.1028 闇討ち
ユーザー brthyyjpbrthyyjp
提出日時 2022-02-21 10:36:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 139,856 KB
最終ジャッジ日時 2023-09-12 01:34:18
合計ジャッジ時間 9,501 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,880 KB
testcase_01 AC 97 ms
71,708 KB
testcase_02 AC 97 ms
71,860 KB
testcase_03 AC 97 ms
71,836 KB
testcase_04 AC 96 ms
71,644 KB
testcase_05 AC 104 ms
76,120 KB
testcase_06 AC 120 ms
77,908 KB
testcase_07 AC 97 ms
71,624 KB
testcase_08 AC 104 ms
76,376 KB
testcase_09 AC 146 ms
79,972 KB
testcase_10 AC 155 ms
81,088 KB
testcase_11 AC 212 ms
89,476 KB
testcase_12 AC 488 ms
132,960 KB
testcase_13 AC 465 ms
131,896 KB
testcase_14 AC 307 ms
105,232 KB
testcase_15 AC 193 ms
86,264 KB
testcase_16 AC 490 ms
139,600 KB
testcase_17 AC 492 ms
139,856 KB
testcase_18 AC 494 ms
139,608 KB
testcase_19 AC 508 ms
139,608 KB
testcase_20 AC 499 ms
139,736 KB
testcase_21 AC 497 ms
139,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

from itertools import accumulate
from collections import defaultdict
INF = 10**18

n = int(input())
A = [list(map(int, input().split())) for i in range(n)]

D = defaultdict(lambda: [])
for x in range(n):
    for y in range(n):
        a = A[x][y]
        D[a-1].append((x, y))

ans = 0
for a in range(n):
    imos = [0]*(n+1)
    imos1 = [0]*(n+1)
    imos2 = [0]*(n+1)
    for x, y in D[a]:
        imos1[0] += 1
        imos1[max(0, x-y)] -= 1
        imos2[min(x+y+1, n)] += 1
        imos2[n] -= 1
    imos1 = list(accumulate(imos1))
    imos2 = list(accumulate(imos2))
    for i in range(n, 0, -1):
        imos1[i-1] += imos1[i]
    for i in range(n):
        imos2[i+1] += imos2[i]
    for i in range(n):
        imos[i] = imos1[i]+imos2[i]
    pos = -1
    M = INF
    for i in range(n):
        if M > imos[i]:
            M = imos[i]
            pos = i
    for x, y in D[a]:
        ans += max(abs(x-pos), y)
print(ans)
0