結果

問題 No.1028 闇討ち
ユーザー tamatotamato
提出日時 2020-04-17 21:52:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,107 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 82,756 KB
実行使用メモリ 150,472 KB
最終ジャッジ日時 2024-04-14 13:32:15
合計ジャッジ時間 5,432 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,892 KB
testcase_01 AC 38 ms
52,800 KB
testcase_02 AC 38 ms
52,740 KB
testcase_03 AC 37 ms
54,048 KB
testcase_04 AC 38 ms
54,152 KB
testcase_05 AC 40 ms
54,460 KB
testcase_06 WA -
testcase_07 AC 39 ms
53,396 KB
testcase_08 AC 39 ms
54,432 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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(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