結果

問題 No.2690 A present from B (Hard)
ユーザー 👑 eoeoeoeo
提出日時 2024-03-16 13:46:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,893 bytes
コンパイル時間 3,786 ms
コンパイル使用メモリ 208,304 KB
実行使用メモリ 814,720 KB
最終ジャッジ日時 2024-03-17 22:30:18
合計ジャッジ時間 5,067 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
6,548 KB
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
const ll LINF = (ll)1e18 + 7;

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

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(m);
    for (ll i = 0; i < m; i++) {
        cin >> a[i];
        a[i]--;
    }

    // present[i][j] := i回目のプレゼントのswapの後で椅子jにあるプレゼントの番号、0-indexed
    vector present(m + 1, vector<ll>(n));

    // 初期状態
    for (ll i = 0; i < n; i++) {
        present[0][i] = i;
    }

    // プレイリストのswap
    for (ll i = 0; i < m; i++) {
        present[i + 1] = present[i];
        swap(present[i + 1][a[i]], present[i + 1][a[i] + 1]);
    }

    // s[i] := 初期状態のあみだくじで、最終的に椅子iに渡るプレゼントの番号、0-indexed
    vector<ll> s = present[m];

    // min_swap_to_s0[i] := s[0]とs[i]とを一気にスワップする最短回数
    vector<ll> min_swap_to_s0(n, LINF);
    min_swap_to_s0[s[0]] = 0;

    // i回目のswap後に、各 0 < j < n に対して、s[0]とs[j]とを一気にスワップする最短回数を求める
    for (ll i = 0; i <= m; i++) {
        ll present_s0 = 0;

        // s[0]の位置を探す
        for (ll j = 0; j < n; j++) {
            if (present[i][j] == s[0]) {
                present_s0 = j;
            }
        }

        // s[0]とs[j]の最短swap回数を求める
        for (ll j = 0; j < n; j++) {
            ll present_num = present[i][j];
            if (present_s0 == present_num) continue;
            chmin(min_swap_to_s0[present_num], abs(present_s0 - present_num));
        }
    }

    for (ll i = 1; i < (ll)min_swap_to_s0.size(); i++) {
        cout << min_swap_to_s0[i] << (i == (ll)min_swap_to_s0.size() - 1 ? "\n" : " ");
    }
}
0