結果

問題 No.2690 A present from B (Hard)
ユーザー 👑 eoeoeoeo
提出日時 2024-03-16 13:46:11
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,893 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 208,760 KB
実行使用メモリ 814,080 KB
最終ジャッジ日時 2024-09-30 04:46:34
合計ジャッジ時間 3,913 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 1 MLE * 1 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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