結果

問題 No.1028 闇討ち
ユーザー yuji9511yuji9511
提出日時 2020-04-17 23:59:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,848 bytes
コンパイル時間 1,771 ms
コンパイル使用メモリ 167,416 KB
実行使用メモリ 28,928 KB
最終ジャッジ日時 2024-04-14 16:16:40
合計ジャッジ時間 4,353 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 9 ms
7,200 KB
testcase_10 AC 11 ms
6,944 KB
testcase_11 AC 40 ms
11,248 KB
testcase_12 AC 159 ms
27,900 KB
testcase_13 AC 158 ms
27,824 KB
testcase_14 AC 88 ms
21,884 KB
testcase_15 AC 30 ms
10,252 KB
testcase_16 AC 169 ms
28,880 KB
testcase_17 AC 169 ms
28,892 KB
testcase_18 AC 169 ms
28,436 KB
testcase_19 AC 168 ms
28,584 KB
testcase_20 AC 168 ms
28,928 KB
testcase_21 AC 169 ms
28,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll A[1010][1010];
    rep(i,0,N){
        rep(j,0,N){
            cin >> A[i][j];
            A[i][j]--;
        }
    }
    if(N == 1){
        print(0);
        return 0;
    }
    vector<lpair> pos[1010];
    rep(i,0,N){
        rep(j,0,N){
            pos[A[i][j]].push_back({i,j});
        }
    }
    ll ans = 0;
    rep(i,0,N){
        ll lb = 0, ub = N+1;
        while(ub - lb > 2){
            ll t1 = (2 * lb + ub) / 3;
            ll t2 = (2 * ub + lb) / 3;
            ll res1 = 0, res2 = 0;
            for(auto &e: pos[i]){
                ll diff_x = abs(t1 - e.first);
                res1 += min(diff_x, e.second) + abs(diff_x - e.second);
                diff_x = abs(t2 - e.first);
                res2 += min(diff_x, e.second) + abs(diff_x - e.second);
            } 
            if(res1 <= res2){
                ub = t2;
            }else{
                lb = t1;
            }
        }
        ll min_val = INF;
        for(ll p = max(0LL, lb-1); p < min(N-1, ub+1); p++){
            ll res = 0;
            for(auto &e: pos[i]){
                ll diff_x = abs(p - e.first);
                res += min(diff_x, e.second) + abs(diff_x - e.second);
            }
            min_val = min(min_val, res);
            
        }
        ans += min_val;

    }
    print(ans);

}
0