結果

問題 No.1051 PQ Permutation
ユーザー pekempeypekempey
提出日時 2020-05-09 18:02:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,121 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 205,844 KB
実行使用メモリ 5,172 KB
最終ジャッジ日時 2023-09-20 03:38:32
合計ジャッジ時間 7,627 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 87 ms
4,812 KB
testcase_16 AC 86 ms
4,900 KB
testcase_17 AC 72 ms
4,896 KB
testcase_18 AC 72 ms
4,908 KB
testcase_19 AC 72 ms
4,872 KB
testcase_20 AC 82 ms
4,872 KB
testcase_21 AC 82 ms
4,968 KB
testcase_22 AC 72 ms
5,096 KB
testcase_23 AC 82 ms
5,172 KB
testcase_24 AC 81 ms
4,812 KB
testcase_25 AC 7 ms
4,376 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 11 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 36 ms
4,380 KB
testcase_36 AC 37 ms
4,376 KB
testcase_37 AC 56 ms
4,376 KB
testcase_38 AC 75 ms
4,872 KB
testcase_39 AC 59 ms
4,960 KB
testcase_40 AC 71 ms
4,896 KB
testcase_41 AC 72 ms
4,936 KB
testcase_42 WA -
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

int index(vector<int> a, int b) {
    return find(range(a), b) - a.begin();
}

void answer(vector<int> a) {
    rep(i, a.size()) cout << a[i] << " \n"[i == a.size() - 1];
    exit(0);
}

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

int main() {
    int N, P, Q; cin >> N >> P >> Q;
    vector<int> A(N); rep(i, N) cin >> A[i];
    if (not next_permutation(range(A))) fail();
    int p = index(A, P);
    int q = index(A, Q);
    if (p < q) answer(A);
    if (P < Q) {
        sort(A.begin() + q + 1, A.end(), greater<>());
        if (not next_permutation(range(A))) fail();
        answer(A);
    } else {
        sort(A.begin() + q + 1, A.end(), greater<>());
        if (not next_permutation(range(A))) fail();
        p = index(A, P);
        q = index(A, Q);
        if (p < q) answer(A);
        while (q < p) {
            swap(A[q], A[q + 1]);
            q++;
        }
        answer(A);
    }
}
0