結果

問題 No.1028 闇討ち
ユーザー 👑 tamatotamato
提出日時 2020-04-17 21:55:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 150,476 KB
最終ジャッジ日時 2024-04-14 14:06:13
合計ジャッジ時間 5,661 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,948 KB
testcase_01 AC 39 ms
53,324 KB
testcase_02 AC 39 ms
54,216 KB
testcase_03 AC 37 ms
52,688 KB
testcase_04 AC 37 ms
52,200 KB
testcase_05 AC 38 ms
54,196 KB
testcase_06 AC 52 ms
65,000 KB
testcase_07 AC 37 ms
53,436 KB
testcase_08 AC 38 ms
54,348 KB
testcase_09 AC 76 ms
78,592 KB
testcase_10 AC 82 ms
79,648 KB
testcase_11 AC 140 ms
88,960 KB
testcase_12 AC 385 ms
141,644 KB
testcase_13 AC 376 ms
141,708 KB
testcase_14 AC 223 ms
109,404 KB
testcase_15 AC 118 ms
86,696 KB
testcase_16 AC 405 ms
149,248 KB
testcase_17 AC 401 ms
149,376 KB
testcase_18 AC 415 ms
150,056 KB
testcase_19 AC 413 ms
150,276 KB
testcase_20 AC 412 ms
150,476 KB
testcase_21 AC 408 ms
149,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    grid = []
    for _ in range(N):
        grid.append(tuple(map(int, input().split())))

    hw = [[] for _ in range(N+1)]
    for h in range(N):
        for w in range(N):
            v = grid[h][w]
            hw[v].append((h, w))

    ans = 0
    for v in range(1, N+1):
        hw_list = hw[v]
        memo_neg = [0] * N
        memo_pos = [0] * N
        for h, w in hw_list:
            ans += w
            if h-w-1 >= 0:
                memo_neg[h-w-1] += 1
            if h+w+1 < N:
                memo_pos[h+w+1] += 1
        A_pos = [0] * N
        cnt = 0
        for i in range(1, N):
            cnt += memo_pos[i]
            A_pos[i] = A_pos[i-1] + cnt
        A_neg = [0] * N
        cnt = 0
        for i in range(N-2, -1, -1):
            cnt += memo_neg[i]
            A_neg[i] = A_neg[i + 1] + cnt
        tmp = 10**9
        for i in range(N):
            tmp = min(tmp, A_pos[i] + A_neg[i])
        ans += tmp
    print(ans)


if __name__ == '__main__':
    main()
0