結果

問題 No.1028 闇討ち
ユーザー pekempeypekempey
提出日時 2020-04-17 22:51:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,097 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 168,888 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-14 15:08:44
合計ジャッジ時間 4,209 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 7 ms
6,944 KB
testcase_11 AC 24 ms
6,940 KB
testcase_12 AC 105 ms
14,336 KB
testcase_13 AC 104 ms
14,336 KB
testcase_14 AC 53 ms
9,344 KB
testcase_15 AC 18 ms
6,944 KB
testcase_16 AC 114 ms
14,976 KB
testcase_17 AC 114 ms
14,976 KB
testcase_18 AC 113 ms
15,104 KB
testcase_19 AC 113 ms
14,976 KB
testcase_20 AC 114 ms
14,976 KB
testcase_21 AC 114 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<vector<int>> A(N, vector<int>(N));
  rep(i, N) rep(j, N) cin >> A[i][j], A[i][j]--;
  vector<vector<int>> S0(N, vector<int>(N + 1));
  vector<vector<int>> S1(N, vector<int>(N + 1));
  auto add = [&](vector<int> &a0, vector<int> &a1, int l, int r, int x0, int x1) {
    a0[l] += x0;
    a0[r] -= x0;
    a1[l] += x1;
    a1[r] -= x1;
  };
  rep(i, N) rep(j, N) {
    int l = max(0, i - j);
    int r = min(N, i + j);
    add(S0[A[i][j]], S1[A[i][j]], 0, l, i, -1);
    add(S0[A[i][j]], S1[A[i][j]], l, r, j, 0);
    add(S0[A[i][j]], S1[A[i][j]], r, N, -i, 1);
  }
  rep(i, N) rep(j, N) {
    S0[i][j + 1] += S0[i][j];
    S1[i][j + 1] += S1[i][j];
  }
  int ans = 0;
  rep(i, N) {
    int mn = INT_MAX;
    rep(j, N) mn = min(mn, S0[i][j] + S1[i][j] * j);
    ans += mn;
  }
  cout << ans << endl;
}
0