結果

問題 No.1028 闇討ち
ユーザー ta7uwta7uw
提出日時 2020-04-17 23:35:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,838 bytes
コンパイル時間 2,167 ms
コンパイル使用メモリ 173,244 KB
実行使用メモリ 34,844 KB
最終ジャッジ日時 2024-04-14 15:41:49
合計ジャッジ時間 6,418 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 36 ms
6,940 KB
testcase_10 AC 60 ms
6,940 KB
testcase_11 AC 439 ms
9,344 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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, 1, N) {
            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