結果

問題 No.1028 闇討ち
ユーザー imoni_kawaiiimoni_kawaii
提出日時 2020-06-09 23:54:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 206,404 KB
実行使用メモリ 16,240 KB
最終ジャッジ日時 2023-09-01 22:37:22
合計ジャッジ時間 6,171 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 6 ms
4,440 KB
testcase_10 AC 8 ms
5,000 KB
testcase_11 AC 24 ms
6,996 KB
testcase_12 AC 90 ms
15,528 KB
testcase_13 AC 89 ms
15,396 KB
testcase_14 AC 52 ms
12,540 KB
testcase_15 AC 19 ms
6,676 KB
testcase_16 AC 93 ms
16,240 KB
testcase_17 AC 94 ms
15,928 KB
testcase_18 AC 95 ms
16,084 KB
testcase_19 AC 94 ms
15,888 KB
testcase_20 AC 95 ms
15,936 KB
testcase_21 AC 95 ms
16,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
using ll = long long;
using ld = long double;
template<class T, class U> inline bool chmax(T &a, const U &b) { if(a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if(a > b) { a = b; return true; } return false; }

using pint = pair<int, int>;
int n, a[1000][1000];

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  cin >> n;
  vector<vector<pint>> pos(n);
  rep(i, n) rep(j, n) cin >> a[i][j], a[i][j]--;
  rep(i, n) {
    rep(j, n) {
      pos[a[i][j]].push_back({i, j});
    }
  }
  ll ans = 0;
  rep(i, n) {
    vector<ll> cd(n+1, 0), cu(n+1, 0);
    rep(j, n) {
      int l = max(0, pos[i][j].first-pos[i][j].second);
      int r = min(n-1, pos[i][j].first+pos[i][j].second);
      cd[r+1]++;
      cu[l]++;
    }
    vector<ll> down(n+1, 0), up(n+1, 0);
    rep(j, n) {
      cd[j+1] += cd[j];
      down[j+1] += down[j]+cd[j+1];
    }
    for(int j = n-1; j >= 0; j--) {
      cu[j] += cu[j+1];
      up[j] += up[j+1]+cu[j+1];
    }
    vector<ll> s(n);
    rep(j, n) s[j] = down[j]+up[j];
    ll d = (1LL<<60);
    rep(j, n) {
      chmin(d, s[j]);
    }
    rep(j, n) d += pos[i][j].second;
    ans += d;
  }
  cout << ans << '\n';
  return 0;
}
0