結果

問題 No.1028 闇討ち
ユーザー IKyoproIKyopro
提出日時 2020-04-17 22:32:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 205,296 KB
実行使用メモリ 31,744 KB
最終ジャッジ日時 2024-04-14 14:49:36
合計ジャッジ時間 6,124 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 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,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 11 ms
6,940 KB
testcase_10 AC 15 ms
6,944 KB
testcase_11 AC 60 ms
9,856 KB
testcase_12 AC 261 ms
30,208 KB
testcase_13 AC 263 ms
30,080 KB
testcase_14 AC 141 ms
19,584 KB
testcase_15 AC 44 ms
8,448 KB
testcase_16 AC 279 ms
31,744 KB
testcase_17 AC 279 ms
31,744 KB
testcase_18 AC 279 ms
31,744 KB
testcase_19 AC 278 ms
31,744 KB
testcase_20 AC 279 ms
31,744 KB
testcase_21 AC 281 ms
31,744 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