結果

問題 No.1051 PQ Permutation
ユーザー MisterMister
提出日時 2020-05-08 22:17:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,368 bytes
コンパイル時間 1,083 ms
コンパイル使用メモリ 86,564 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2023-09-19 07:42:03
合計ジャッジ時間 7,711 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,476 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 51 ms
4,908 KB
testcase_16 AC 50 ms
4,924 KB
testcase_17 AC 36 ms
4,380 KB
testcase_18 AC 36 ms
4,376 KB
testcase_19 AC 37 ms
4,428 KB
testcase_20 AC 46 ms
4,940 KB
testcase_21 AC 46 ms
5,036 KB
testcase_22 AC 36 ms
4,376 KB
testcase_23 AC 45 ms
4,968 KB
testcase_24 AC 45 ms
4,972 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 28 ms
4,380 KB
testcase_36 AC 20 ms
4,392 KB
testcase_37 AC 19 ms
4,384 KB
testcase_38 AC 39 ms
4,972 KB
testcase_39 AC 21 ms
4,972 KB
testcase_40 AC 33 ms
4,404 KB
testcase_41 AC 36 ms
5,104 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

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);

    while (true) {
        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) {
                std::sort(bs.rbegin(), bs.rend());
                std::copy(bs.begin(), bs.end(), xs.begin() + qi);

                if (!std::next_permutation(xs.begin(), xs.end())) fail();
                if (pos(xs, p) < pos(xs, q)) answer(xs);
                continue;
            }

            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