結果

問題 No.2673 A present from B
ユーザー 👑 eoeoeoeo
提出日時 2024-02-11 21:49:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,893 bytes
コンパイル時間 2,004 ms
コンパイル使用メモリ 208,304 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-13 21:50:37
合計ジャッジ時間 2,895 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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