結果
| 問題 | 
                            No.1028 闇討ち
                             | 
                    
| コンテスト | |
| ユーザー | 
                             brthyyjp
                         | 
                    
| 提出日時 | 2022-02-21 10:36:28 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 348 ms / 2,000 ms | 
| コード長 | 1,017 bytes | 
| コンパイル時間 | 217 ms | 
| コンパイル使用メモリ | 82,036 KB | 
| 実行使用メモリ | 139,204 KB | 
| 最終ジャッジ日時 | 2024-06-29 14:44:16 | 
| 合計ジャッジ時間 | 5,943 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 20 | 
ソースコード
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)
            
            
            
        
            
brthyyjp