結果

問題 No.1028 闇討ち
ユーザー IKyoproIKyopro
提出日時 2020-04-17 22:32:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 209,616 KB
実行使用メモリ 31,620 KB
最終ジャッジ日時 2023-07-27 01:27:56
合計ジャッジ時間 6,399 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 11 ms
4,720 KB
testcase_10 AC 15 ms
4,980 KB
testcase_11 AC 59 ms
9,580 KB
testcase_12 AC 261 ms
30,220 KB
testcase_13 AC 260 ms
29,916 KB
testcase_14 AC 137 ms
19,356 KB
testcase_15 AC 43 ms
8,388 KB
testcase_16 AC 281 ms
31,484 KB
testcase_17 AC 280 ms
31,492 KB
testcase_18 AC 282 ms
31,612 KB
testcase_19 AC 280 ms
31,620 KB
testcase_20 AC 281 ms
31,616 KB
testcase_21 AC 280 ms
31,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0