結果

問題 No.1051 PQ Permutation
ユーザー MisterMister
提出日時 2020-05-08 21:59:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,079 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 83,184 KB
実行使用メモリ 5,128 KB
最終ジャッジ日時 2023-09-19 07:36:59
合計ジャッジ時間 4,278 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 52 ms
5,052 KB
testcase_16 AC 51 ms
4,976 KB
testcase_17 AC 36 ms
4,376 KB
testcase_18 AC 36 ms
4,376 KB
testcase_19 AC 36 ms
4,380 KB
testcase_20 AC 46 ms
4,912 KB
testcase_21 AC 46 ms
4,896 KB
testcase_22 AC 36 ms
4,376 KB
testcase_23 AC 45 ms
5,128 KB
testcase_24 AC 46 ms
4,896 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 6 ms
4,388 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 WA -
testcase_36 AC 20 ms
4,380 KB
testcase_37 AC 19 ms
4,388 KB
testcase_38 AC 39 ms
4,920 KB
testcase_39 AC 19 ms
4,928 KB
testcase_40 AC 33 ms
4,380 KB
testcase_41 AC 36 ms
4,996 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 20 ms
5,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>

void fail() {
    std::cout << -1 << std::endl;
    std::exit(0);
}

void answer(const std::vector<int>& xs) {
    for (auto x : xs) std::cout << x << " ";
    std::cout << std::endl;
    std::exit(0);
}

void answer(const std::vector<int>& fs, const std::vector<int>& bs) {
    for (auto x : fs) std::cout << x << " ";
    for (auto x : bs) std::cout << x << " ";
    std::cout << std::endl;
    std::exit(0);
}

int pos(const std::vector<int>& xs, int x) {
    return std::find(xs.begin(), xs.end(), x) - xs.begin();
}

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

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

    if (!std::next_permutation(xs.begin(), xs.end())) fail();

    if (pos(xs, p) < pos(xs, q)) answer(xs);

    int qi = pos(xs, q);

    std::vector<int>
        fs(xs.begin(), xs.begin() + qi),
        bs(xs.begin() + qi, xs.end());
    int m = bs.size();

    if (q > p) {
        int min = n + 1;
        for (int i = 0; i < m; ++i) {
            if (bs[i] > q) min = std::min(min, bs[i]);
        }

        if (min > n) {
            if (!std::next_permutation(fs.begin(), fs.end())) fail();
            std::sort(bs.begin(), bs.end());
            answer(fs, bs);
        }

        auto ri = pos(bs, min);
        std::swap(bs.front(), bs[ri]);
        std::sort(bs.begin() + 1, bs.end());
        answer(fs, bs);

    } else {
        int min = n + 1;
        for (int i = 0; i < m; ++i) {
            if (bs[i] > q) min = std::min(min, bs[i]);
        }
        assert(min <= n);

        auto ri = pos(bs, min);
        std::swap(bs.front(), bs[ri]);
        std::sort(bs.begin() + 1, bs.end());

        if (pos(bs, p) < pos(bs, q)) answer(fs, bs);
        auto pi = pos(bs, p), qi = pos(bs, q);
        while (pi > qi) {
            std::swap(bs[qi], bs[qi + 1]);
            ++qi;
        }

        answer(fs, bs);
    }
}

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

    solve();

    return 0;
}
0