結果

問題 No.1051 PQ Permutation
ユーザー kakira9618kakira9618
提出日時 2020-04-05 23:37:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,052 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 179,388 KB
実行使用メモリ 14,728 KB
最終ジャッジ日時 2023-09-19 07:31:02
合計ジャッジ時間 8,017 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 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 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 RE -
testcase_10 AC 2 ms
4,376 KB
testcase_11 RE -
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 229 ms
14,660 KB
testcase_16 AC 209 ms
14,152 KB
testcase_17 AC 77 ms
5,372 KB
testcase_18 AC 78 ms
5,420 KB
testcase_19 AC 78 ms
5,416 KB
testcase_20 AC 144 ms
10,916 KB
testcase_21 AC 156 ms
11,264 KB
testcase_22 AC 77 ms
5,372 KB
testcase_23 AC 139 ms
11,008 KB
testcase_24 AC 134 ms
10,836 KB
testcase_25 AC 10 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 12 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 11 ms
4,376 KB
testcase_31 AC 3 ms
4,384 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 65 ms
8,864 KB
testcase_36 AC 65 ms
8,884 KB
testcase_37 AC 147 ms
14,708 KB
testcase_38 RE -
testcase_39 AC 129 ms
14,712 KB
testcase_40 AC 119 ms
10,308 KB
testcase_41 RE -
testcase_42 AC 2 ms
4,376 KB
testcase_43 RE -
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 RE -
testcase_48 AC 207 ms
14,728 KB
権限があれば一括ダウンロードができます

ソースコード

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]) || (!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) {
    assert(A.size() == B.size() && B.size() >= 2);
    if (A == B) return;
    assert(A < B);
    int N = A.size();
    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