結果

問題 No.1051 PQ Permutation
ユーザー kakira9618kakira9618
提出日時 2020-04-06 00:04:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,305 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 178,756 KB
実行使用メモリ 14,956 KB
最終ジャッジ日時 2023-09-19 07:31:54
合計ジャッジ時間 9,426 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 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,380 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 10 ms
4,376 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 12 ms
4,376 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 11 ms
4,380 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 11 ms
4,384 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 66 ms
8,984 KB
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,504 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,384 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(), (x).end()

vector<int> solve(vector<int> &A, int P, int Q) {
    int N = A.size();
    vector<int> B = A, A_i(N + 1);
    for(int i = 0; i < N; i++) {
        A_i[A[i]] = i;
    }
    bool p = false, q = false;
    set<int> S;
    for(int i = N - 1; i >= 0; i--) {
        p |= A[i] == P;
        q |= A[i] == Q;
        if (((A_i[P] < A_i[Q] && A_i[Q] < i) || ((!p) && q)) && S.lower_bound(A[i]) != S.end()) {
            int m = *S.lower_bound(A[i]);
            swap(B[i], B[A_i[m]]);
            sort(B.begin() + i + 1, B.end());
            break;
        }
        if (p && q && S.lower_bound(A[i]) != S.end()) {
            int m = *S.lower_bound(A[i]);
            if (m == Q) {
                if (next(S.lower_bound(A[i]), 1) == S.end()) {
                    S.insert(A[i]);
                    continue;
                }
                m = *next(S.lower_bound(A[i]), 1);
            }
            swap(B[i], B[A_i[m]]);
            sort(B.begin() + i + 1, B.end());
            if (P > Q && m != P) {
                auto p_it = find(all(B), P);
                auto q_it = find(all(B), Q);
                rotate(q_it, q_it + 1, p_it + 1);
            }
            break;
        }
        S.insert(A[i]);
    }
    return B;
}

void validate(vector<int> &A, vector<int> &B, int P, int Q) {
    int N = A.size();
    assert(N >= 2 && N <= 100000);
    assert(P >= 1 && P <= N && Q >= 1 && Q <= N);
    assert(P != N);
    vector<int> A_i(N + 1, -1);
    for(int i = 0; i < N; i++) {
        assert(A_i[A[i]] == -1);
        A_i[A[i]] = i;
    }
    
    assert(A.size() == B.size() && B.size() >= 2);
    if (A == B) return;
    assert(A < B);
    vector<int> B_i(N + 1, -1);
    for(int i = 0; i < N; i++) {
        assert(B_i[B[i]] == -1);
        B_i[B[i]] = i;
    }
    assert(B_i[P] < B_i[Q]);
}

int main() {
    int N, P, Q;
    cin >> N >> P >> Q;
    vector<int> A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<int> B = solve(A, P, Q);
    validate(A, B, P, Q);
    if (A == B) {
        cout << -1 << endl;
    } else {
        for(int i = 0; i < N; i++) {
            cout << B[i];
            if (i != N - 1) cout << " "; else cout << endl;
        }
    }
    return 0;
}
0