結果

問題 No.1028 闇討ち
ユーザー oevloevl
提出日時 2020-04-18 13:53:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 865 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 201,028 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-04-14 20:48:04
合計ジャッジ時間 5,905 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 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 12 ms
6,940 KB
testcase_10 AC 16 ms
6,940 KB
testcase_11 AC 63 ms
6,944 KB
testcase_12 AC 264 ms
11,904 KB
testcase_13 AC 265 ms
11,776 KB
testcase_14 AC 146 ms
9,600 KB
testcase_15 AC 47 ms
6,940 KB
testcase_16 AC 280 ms
12,032 KB
testcase_17 AC 285 ms
12,032 KB
testcase_18 AC 281 ms
12,160 KB
testcase_19 AC 282 ms
12,160 KB
testcase_20 AC 286 ms
12,160 KB
testcase_21 AC 285 ms
12,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int, int>;

int main() {
    int N; cin >> N;
    vector<vector<Pair>> pos(N);
    for(int i = 0; i < N; ++i) {
        for(int j = 0; j < N; ++j) {
            int a; cin >> a; a--;
            pos[a].emplace_back(i, j);
        }
    }
    int ans = 0;
    for(int a = 0; a < N; ++a) {
        auto f = [&](int x) -> int {
            int res = 0;
            for(auto &[i, j] : pos[a]) res += max(abs(x - i), j);
            return res;
        };
        int l = 0, r = N - 1;
        while(l + 2 < r) {
            int c1 = (l * 2 + r) / 3;
            int c2 = (l + r * 2) / 3;
            if(f(c1) > f(c2)) l = c1;
            else r = c2;
        }
        int mn = f(r);
        for(int i = l; i < r; ++i) mn = min(mn, f(i));
        ans += mn;
    }
    cout << ans << '\n';
    return 0;
}
0