結果

問題 No.1028 闇討ち
ユーザー beetbeet
提出日時 2020-04-17 21:57:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,211 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 200,836 KB
実行使用メモリ 28,884 KB
最終ジャッジ日時 2024-04-14 14:08:04
合計ジャッジ時間 13,552 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 15 ms
6,940 KB
testcase_10 AC 24 ms
7,112 KB
testcase_11 AC 146 ms
12,744 KB
testcase_12 AC 1,105 ms
28,008 KB
testcase_13 AC 1,091 ms
27,812 KB
testcase_14 AC 464 ms
22,192 KB
testcase_15 AC 97 ms
10,252 KB
testcase_16 AC 1,201 ms
28,708 KB
testcase_17 AC 1,206 ms
28,820 KB
testcase_18 AC 1,210 ms
28,884 KB
testcase_19 AC 1,211 ms
28,864 KB
testcase_20 AC 1,210 ms
28,484 KB
testcase_21 AC 1,209 ms
28,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';

//INSERT ABOVE HERE
const Int MAX = 1010;
Int A[MAX][MAX];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  for(Int i=0;i<n;i++)
    for(Int j=0;j<n;j++)
      cin>>A[i][j],A[i][j]--;

  using P = pair<Int, Int>;
  vector<vector<P>> vp(n);
  for(Int i=0;i<n;i++)
    for(Int j=0;j<n;j++)
      vp[A[i][j]].emplace_back(i,j);

  const Int INF = 1e9;
  Int ans=0;
  for(Int i=0;i<n;i++){
    Int res=INF;
    for(Int j=0;j<n;j++){
      Int sum=0;
      for(auto p:vp[i]){
        sum+=max(abs(p.first-j),p.second);
        if(res<sum) break;
      }
      chmin(res,sum);
    }
    ans+=res;
  }
  cout<<ans<<newl;
  return 0;
}
0