結果

問題 No.1028 闇討ち
ユーザー ta7uwta7uw
提出日時 2020-04-17 23:09:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 682 ms / 2,000 ms
コード長 1,850 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 173,124 KB
実行使用メモリ 28,800 KB
最終ジャッジ日時 2024-04-14 15:27:49
合計ジャッジ時間 9,680 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 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 28 ms
6,940 KB
testcase_10 AC 41 ms
6,940 KB
testcase_11 AC 155 ms
9,344 KB
testcase_12 AC 640 ms
27,776 KB
testcase_13 AC 634 ms
27,520 KB
testcase_14 AC 351 ms
19,840 KB
testcase_15 AC 116 ms
8,064 KB
testcase_16 AC 680 ms
28,544 KB
testcase_17 AC 678 ms
28,672 KB
testcase_18 AC 676 ms
28,800 KB
testcase_19 AC 677 ms
28,672 KB
testcase_20 AC 682 ms
28,672 KB
testcase_21 AC 677 ms
28,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    ll N;
    cin >> N;
    vector<vector<ll>> grid(N, vector<ll>(N));
    vector<ll> cnt(N, 0);
    vector<vector<P>> point(N);
    rep(i, N) {
        rep(j, N) {
            ll a;
            cin >> a;
            grid[i][j] = a;
            cnt[a - 1] += i + 1;
            point[a - 1].emplace_back(i + 1, j + 1);
        }
    }
    ll ans = 0;
    rep(t, N) {
        ll h = cnt[t];
        h /= N;
        ll M = INF;
        rep2(h2, h - 100, h + 100) {
            ll ans2 = 0;
            rep(j, N) {
                P p = point[t][j];
                if (p.first == h2) {
                    ans2 += p.second - 1;
                } else if (p.second == 1) {
                    ans2 += abs(p.first - h2);
                } else {
                    ll f = abs(1 - p.second);
                    ll s = abs(p.first - h2);
                    ll m = min(f, s);
                    ll add = m + abs(f - m) + abs(s - m);
                    ans2 += add;
                }
            }
            M = min(M, ans2);
        }
        ans += M;
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}
0