結果

問題 No.1028 闇討ち
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2020-04-17 22:20:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,479 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 170,960 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-14 14:37:38
合計ジャッジ時間 5,493 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 11 ms
6,944 KB
testcase_10 AC 16 ms
6,940 KB
testcase_11 AC 61 ms
6,940 KB
testcase_12 AC 258 ms
11,904 KB
testcase_13 AC 256 ms
11,904 KB
testcase_14 AC 141 ms
9,600 KB
testcase_15 AC 46 ms
6,940 KB
testcase_16 AC 273 ms
12,032 KB
testcase_17 AC 276 ms
12,160 KB
testcase_18 AC 275 ms
12,032 KB
testcase_19 AC 277 ms
12,288 KB
testcase_20 AC 275 ms
12,160 KB
testcase_21 AC 275 ms
12,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*

https://yukicoder.me/problems/no/1028

難読なだけ?
ドローンの移動回数はx座標とy座標の差の最大値
凸関数っぽいので三分探索できる

計算量はO(N*N*logN)

*/

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n,m) for(int (i)=(n);(i)<(m);(i)++)
#define rrep(i,n,m) for(int (i)=(n);(i)>(m);(i)--)
using ll = long long;
const ll mod = 998244353;

int main(){

    int N;
    cin >> N;

    vector<vector<pair<int,int>>> a(N,vector<pair<int,int>> (0));

    int na;
    rep(i,0,N){
        rep(j,0,N){
            cin >> na;
            a[na-1].push_back(make_pair(i,j));
        }
    }

    int ans = 0,l,r,m1,m2,s1,s2;

    rep(i,0,N){
        
        l = 0;
        r = N+1;

        while (r-l > 3){
            
            m1 = (l*2+r)/3;
            m2 = (l+r*2)/3;
            s1 = 0;
            s2 = 0;

            rep (j,0,N){
                s1 += max(abs(m1-a[i][j].first) , a[i][j].second);
            }

            rep (j,0,N){
                s2 += max(abs(m2-a[i][j].first) , a[i][j].second);
            }

            if (s1 > s2){
                l = m1;
            }else{
                r = m2;
            }
        }

        int rmin = 1000000000;
        rep(k,l-2,r+2){
            s1 = 0;
            rep (j,0,N){
                s1 += max(abs(k-a[i][j].first) , a[i][j].second);
            }
            rmin = min(rmin,s1);
        }

        ans += rmin;
    }

    cout << ans << endl;
}
0