結果

問題 No.1028 闇討ち
ユーザー SSRSSSRS
提出日時 2020-04-17 22:10:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 1,249 bytes
コンパイル時間 1,825 ms
コンパイル使用メモリ 172,948 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2024-04-14 14:23:08
合計ジャッジ時間 5,197 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 11 ms
6,940 KB
testcase_10 AC 15 ms
6,944 KB
testcase_11 AC 55 ms
6,944 KB
testcase_12 AC 222 ms
15,616 KB
testcase_13 AC 221 ms
15,488 KB
testcase_14 AC 124 ms
11,648 KB
testcase_15 AC 41 ms
6,944 KB
testcase_16 AC 238 ms
16,000 KB
testcase_17 AC 235 ms
16,128 KB
testcase_18 AC 239 ms
16,128 KB
testcase_19 AC 238 ms
16,000 KB
testcase_20 AC 238 ms
16,000 KB
testcase_21 AC 237 ms
16,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cin >> A[i][j];
      A[i][j]--;
    }
  }
  vector<vector<pair<int, int>>> P(N);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      P[A[i][j]].push_back(make_pair(i, j));
    }
  }
  long long total = 0;
  for (int i = 0; i < N; i++){
    //社員iに攻撃
    //1行目のコスト
    int row1 = 0;
    for (int j = 0; j < N; j++){
      row1 += max(P[i][j].first, P[i][j].second);
    }
    //1次imos
    vector<int> d(N + 1, 0);
    for (int j = 0; j < N; j++){
      int r = P[i][j].first;
      int c = P[i][j].second;
      if (r - c > 0){
        //減少
        d[1]--;
        if (r - c + 1 <= N){
          d[r - c + 1]++;
        }
      }
      if (r + c < N - 1){
        //増加
        d[r + c + 1]++;
        d[N]--;
      }
    }
    //累積和
    for (int j = 0; j < N; j++){
      d[j + 1] += d[j];
    }
    for (int j = 0; j < N; j++){
      d[j + 1] += d[j];
    }
    int ans = row1;
    for (int j = 0; j < N; j++){
      ans = min(ans, row1 + d[j]);
    }
    total += ans;
  }
  cout << total << endl;
}
0