結果
問題 | No.1028 闇討ち |
ユーザー |
![]() |
提出日時 | 2020-04-17 22:32:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 340 ms / 2,000 ms |
コード長 | 1,914 bytes |
コンパイル時間 | 2,949 ms |
コンパイル使用メモリ | 203,388 KB |
最終ジャッジ日時 | 2025-01-09 20:20:02 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using Pa = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vvec<int> A(N,vec<int>(N,0)); for(int i=0;i<N;i++) for(int j=0;j<N;j++){ cin >> A[i][j]; A[i][j]--; } auto f = [&](int x,int y,bool isx){ if(isx) return x+y; else return x-y; }; vvec<int> X(N); vvec<int> Y(N); for(int i=0;i<N;i++) for(int j=0;j<N;j++){ X[A[i][j]].push_back(f(i,j,1)); Y[A[i][j]].push_back(f(i,j,0)); } for(int i=0;i<N;i++){ sort(X[i].begin(),X[i].end()); sort(Y[i].begin(),Y[i].end()); } auto calc = [&](vvec<int>& A){ vvec<int> L(N,vec<int>(N+1,0)),R = L; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ L[i][j+1] = L[i][j]+A[i][j]; } for(int j=N-1;j>=0;j--){ R[i][j] = R[i][j+1]+A[i][j]; } } int inf = 2e9; vvec<int> ans(N,vec<int>(N,inf)); for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ int id = lower_bound(A[i].begin(),A[i].end(),j)-A[i].begin(); ans[i][j] = id*j-L[i][id]+R[i][id]-(N-id)*j; } } /* for(int i=0;i<N;i++){ cerr << "i : " << i << "\n"; for(int j=0;j<N;j++) cerr << ans[i][j] << (j!=N-1? " ":"\n"); for(int j=0;j<N;j++) cerr << L[i][j+1] << (j!=N-1? " ":"\n"); }*/ return ans; }; auto ansX = calc(X); auto ansY = calc(Y); ll ans = 0; for(int i=0;i<N;i++){ int now = 1e9; for(int j=0;j<N;j++) now = min(now,ansX[i][j]+ansY[i][j]); ans += now; } cout << ans/2 << "\n"; }