結果

問題 No.1028 闇討ち
ユーザー knshnb
提出日時 2020-04-17 21:46:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 591 ms / 2,000 ms
コード長 1,742 bytes
コンパイル時間 3,250 ms
コンパイル使用メモリ 200,760 KB
最終ジャッジ日時 2025-01-09 19:53:30
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Fri Apr 17 21:39:42 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
    return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

template <class T> inline bool chmin(T& a, const T& b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <class T> inline bool chmax(T& a, const T& b) {
    if (a >= b) return false;
    a = b;
    return true;
}

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> offset(n);
    auto t = make_vec<Int>(n, n, 2, 0);
    REP(i, n) {
        REP(j, n) {
            Int x;
            std::cin >> x, x--;
            offset[x] += j;
            if (i - j - 1 >= 0) t[x][i - j - 1][0]++;
            if (i + j + 1 < n) t[x][i + j + 1][1]++;
        }
    }
    Int ans = 0;
    REP(i, n) {
        REP(_, 2) {
            for (Int j = n - 2; j >= 0; j--) {
                t[i][j][0] += t[i][j + 1][0];
            }
            REP(j, n - 1) {
                t[i][j + 1][1] += t[i][j][1];
            }
        }
        Int mi = 1e9;
        REP(j, n) {
            chmin(mi, offset[i] + t[i][j][0] + t[i][j][1]);
        }
        ans += mi;
    }
    std::cout << ans << std::endl;
}
0