結果

問題 No.1051 PQ Permutation
ユーザー finefine
提出日時 2020-05-09 01:01:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,739 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 172,812 KB
実行使用メモリ 4,692 KB
最終ジャッジ日時 2023-09-19 08:15:16
合計ジャッジ時間 5,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 66 ms
4,624 KB
testcase_16 AC 66 ms
4,672 KB
testcase_17 AC 50 ms
4,688 KB
testcase_18 AC 50 ms
4,632 KB
testcase_19 AC 50 ms
4,620 KB
testcase_20 AC 60 ms
4,692 KB
testcase_21 AC 60 ms
4,628 KB
testcase_22 AC 51 ms
4,624 KB
testcase_23 AC 59 ms
4,552 KB
testcase_24 AC 60 ms
4,676 KB
testcase_25 AC 6 ms
4,384 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 8 ms
4,384 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 6 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 6 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 20 ms
4,384 KB
testcase_36 AC 19 ms
4,380 KB
testcase_37 AC 23 ms
4,560 KB
testcase_38 AC 39 ms
4,624 KB
testcase_39 AC 23 ms
4,680 KB
testcase_40 AC 43 ms
4,620 KB
testcase_41 AC 36 ms
4,668 KB
testcase_42 WA -
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 1 ms
4,384 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,384 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 24 ms
4,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

bool judge(const vector<int>& a, int p, int q) {
    for (int x : a) {
        if (x == q) return false;
        if (x == p) return true;
    }
    assert(false);
}

bool judge2(const vector<int>& a, int q, int& hoge) {
    int n = a.size();
    hoge = n + 1;
    bool res = true;
    for (int i = n - 1; i >= 0; i--) {
        if (a[i] == q) return res;
        if (a[i] > q) {
            res = false;
            hoge = min(hoge, a[i]);
        }
    }
    assert(false);
}

void func1(vector<int>& a, int q, int hoge) {
    int n = a.size();

    int idx = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == q) {
            a[i] = hoge;
            idx = i;
        } else if (a[i] == hoge) a[i] = q;
    }
    sort(a.begin() + idx + 1, a.end());
}

void func2(vector<int>& a, int p, int q) {
    int n = a.size();

    for (int i = 0; i < n; i++) {
        if (a[i] == q) {
            assert(i + 1 < n);
            swap(a[i], a[i + 1]);
            if (a[i] == p) return;
        }
    }
}

int func3(const vector<int>& a, int q) {
    int n = a.size();
    int idx = -1;
    for (int i = 0; i < n; ++i) {
        if (a[i] == q) return idx;
        if (a[i] < q) idx = i;
    }
    return idx;
}

void print(const vector<int>& a) {
    int n = a.size();
    for (int i = 0; i < n; i++) {
        cout << a[i] << " \n"[i + 1 == n];
    }
}

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

    int n, p, q;
    cin >> n >> p >> q;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> aa = a;
    sort(aa.begin(), aa.end(), greater<>());

    if (a == aa) {
        cout << -1 << newl;
        return 0;
    }
    next_permutation(a.begin(), a.end());

    if (judge(a, p, q)) {
        print(a);
        return 0;
    }

    int hoge;
    if (!judge2(a, q, hoge)) {
        func1(a, q, hoge);
        if (judge(a, p, q)) {
            print(a);
            return 0;
        }

        func2(a, p, q);
        if (judge(a, p, q)) {
            print(a);
            return 0;
        } else {
            cout << -1 << newl;
            return 0;
        }
    } else {
        int idx = func3(a, q);
        if (idx == -1) {
            cout << -1 << newl;
            return 0;
        }

        if (!judge2(a, a[idx], hoge)) {
            func1(a, a[idx], hoge);
            if (judge(a, p, q)) {
                print(a);
                return 0;
            } else {
                cout << -1 << newl;
                return 0;
            }
        } else {
            cout << -1 << newl;
            return 0;
        }

    }
    return 0;
}
0