結果

問題 No.1028 闇討ち
ユーザー beet
提出日時 2020-04-17 21:57:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,872 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 197,776 KB
最終ジャッジ日時 2025-01-09 19:58:45
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

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