結果

問題 No.326 あみだますたー
ユーザー Mister
提出日時 2020-05-29 16:52:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,001 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 76,236 KB
最終ジャッジ日時 2025-01-10 16:10:24
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector<std::pair<int, int>> ps(k);
    for (auto& p : ps) {
        std::cin >> p.first >> p.second;
        --p.first, --p.second;
    }

    std::vector<int> xs(n);
    for (auto& x : xs) {
        std::cin >> x;
        --x;
    }

    for (auto p : ps) {
        std::swap(xs[p.first], xs[p.second]);
    }

    std::vector<std::pair<int, int>> ans;

    bool update = true;
    while (update) {
        update = false;
        for (int i = 0; i + 1 < n; ++i) {
            if (xs[i] > xs[i + 1]) {
                ans.emplace_back(i, i + 1);
                std::swap(xs[i], xs[i + 1]);
                update = true;
            }
        }
    }

    std::cout << ans.size() << "\n";
    for (auto p : ans) {
        std::cout << p.first + 1 << " " << p.second + 1 << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0