結果

問題 No.1028 闇討ち
ユーザー tnakao0123tnakao0123
提出日時 2020-04-19 02:33:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 94,408 KB
実行使用メモリ 11,732 KB
最終ジャッジ日時 2024-04-14 21:26:35
合計ジャッジ時間 3,279 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 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 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 7 ms
6,940 KB
testcase_10 AC 9 ms
6,944 KB
testcase_11 AC 30 ms
9,488 KB
testcase_12 AC 115 ms
11,596 KB
testcase_13 AC 114 ms
11,348 KB
testcase_14 AC 64 ms
10,440 KB
testcase_15 AC 23 ms
9,420 KB
testcase_16 AC 123 ms
11,480 KB
testcase_17 AC 123 ms
11,604 KB
testcase_18 AC 123 ms
11,724 KB
testcase_19 AC 124 ms
11,492 KB
testcase_20 AC 123 ms
11,732 KB
testcase_21 AC 124 ms
11,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1028.cc:  No.1028 闇討ち - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int xs[MAX_N][MAX_N], ys[MAX_N][MAX_N], cs[MAX_N];
int decs[MAX_N + 1], incs[MAX_N + 1];

/* subroutines */

inline int dist(int dx, int dy) { return max(abs(dx), dy); }

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++) {
      int a;
      scanf("%d", &a);
      a--;
      xs[a][cs[a]] = x;
      ys[a][cs[a]] = y;
      cs[a]++;
    }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    memset(decs, 0, sizeof(decs));
    memset(incs, 0, sizeof(incs));
    for (int j = 0; j < n; j++) {
      int y0 = ys[i][j] - xs[i][j], y1 = ys[i][j] + xs[i][j];
      if (y0 > 0) decs[0]++, decs[y0]--;
      if (y1 + 1 < n) incs[y1 + 1]++, incs[n]--;
    }
    for (int y = 0; y < n; y++)
      decs[y + 1] += decs[y], incs[y + 1] += incs[y];

    int s = 0;
    for (int j = 0; j < n; j++) s += dist(xs[i][j], ys[i][j]);
    int mins = s;

    for (int y = 1; y < n; y++) {
      s += incs[y] - decs[y - 1];
      if (mins > s) mins = s;
    }
    sum += mins;
    //printf("%d: %d\n", i, mins);
  }

  printf("%d\n", sum);
  return 0;
}
0