結果

問題 No.1028 闇討ち
ユーザー yutasyutas
提出日時 2020-04-17 22:27:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,739 ms / 2,000 ms
コード長 1,375 bytes
コンパイル時間 1,008 ms
コンパイル使用メモリ 98,044 KB
実行使用メモリ 16,284 KB
最終ジャッジ日時 2024-04-14 14:44:28
合計ジャッジ時間 16,864 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 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,944 KB
testcase_09 AC 20 ms
6,940 KB
testcase_10 AC 31 ms
6,940 KB
testcase_11 AC 205 ms
7,424 KB
testcase_12 AC 1,586 ms
15,812 KB
testcase_13 AC 1,564 ms
15,648 KB
testcase_14 AC 654 ms
12,524 KB
testcase_15 AC 135 ms
7,040 KB
testcase_16 AC 1,730 ms
16,196 KB
testcase_17 AC 1,728 ms
16,284 KB
testcase_18 AC 1,739 ms
16,132 KB
testcase_19 AC 1,739 ms
16,128 KB
testcase_20 AC 1,731 ms
16,124 KB
testcase_21 AC 1,735 ms
16,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

int N, A[1005][1005], start[1005];
vector <Pii> P[1005];

int Dist(int i1, int j1, int i2, int j2){
  int now = abs(i2 - i1);
  if (j1 - now <= j2 && j2 <= j1 + now){
    return now;
  }
  
  if (j2 < j1 - now){
    return now + (j1 - now - j2);
  }else{
    return now + (j2 - j1 - now);
  }
}

int main(){
  scanf("%d", &N);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      scanf("%d", &A[i][j]);
      P[A[i][j]].push_back(make_pair(i, j));
    }
  }

  int ans = 0;
  for (int i = 1; i <= N; i++){
    int now = 1e9;
    for (int k = 0; k < N; k++){
      int sum = 0;
      for (int j = 0; j < N; j++){
        sum += Dist(k, 0, P[i][j].first, P[i][j].second);
      }
      now = min(now, sum);
    }
    ans += now;
  }
  cout << ans << endl;  

  return 0;
}
0