結果

問題 No.1051 PQ Permutation
ユーザー merom686merom686
提出日時 2020-05-09 00:56:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 1,154 ms
コンパイル使用メモリ 93,376 KB
実行使用メモリ 4,396 KB
最終ジャッジ日時 2023-09-19 08:14:31
合計ジャッジ時間 4,670 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,384 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,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 48 ms
4,380 KB
testcase_16 AC 48 ms
4,392 KB
testcase_17 AC 34 ms
4,384 KB
testcase_18 AC 34 ms
4,380 KB
testcase_19 AC 33 ms
4,384 KB
testcase_20 AC 43 ms
4,380 KB
testcase_21 AC 43 ms
4,380 KB
testcase_22 AC 33 ms
4,380 KB
testcase_23 AC 42 ms
4,384 KB
testcase_24 AC 43 ms
4,380 KB
testcase_25 AC 4 ms
4,384 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 6 ms
4,384 KB
testcase_29 AC 3 ms
4,384 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 18 ms
4,380 KB
testcase_36 AC 18 ms
4,380 KB
testcase_37 AC 19 ms
4,396 KB
testcase_38 AC 35 ms
4,384 KB
testcase_39 AC 19 ms
4,380 KB
testcase_40 AC 31 ms
4,380 KB
testcase_41 AC 33 ms
4,388 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,388 KB
testcase_48 AC 20 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:48:25: 警告: ‘i1’ may be used uninitialized [-Wmaybe-uninitialized]
   48 |         for (int i = i1 + 1; i < n; i++) {
      |                      ~~~^~~
main.cpp:37:18: 備考: ‘i1’ はここで定義されています
   37 |     int i0 = -1, i1;
      |                  ^~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

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

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

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }

    if (!next_permutation(a.begin(), a.end())) {
        cout << -1 << endl;
        exit(0);
    }

    auto f = [&]() {
        for (int i = 0; i < n; i++) {
            cout << a[i] + 1 << " \n"[i == n - 1];
        }
        exit(0);
    };

    int i0 = -1, i1;
    for (int i = 0; i < n; i++) {
        if (a[i] == p) i0 = i;
        if (a[i] == q) {
            if (i0 >= 0) f();
            i1 = i;
        }
    }

    if (p > q) {
        pair<int, int> a1 = { p, i0 };
        for (int i = i1 + 1; i < n; i++) {
            if (a[i] > q) a1 = min(a1, { a[i], i });
        }
        if (a1.first == p) {
            swap(a[i1], a[i0]);
            sort(a.begin() + i1 + 1, a.end());

        } else {
            swap(a[i1], a[a1.second]);
            sort(a.begin() + i1 + 1, a.end());
            for (int i = i1 + 1; i < n; i++) {
                if (a[i] == q) i0 = i;
                if (a[i] == p) {
                    rotate(a.begin() + i0, a.begin() + i0 + 1, a.begin() + i + 1);
                }
            }
        }
        f();

    } else {
        pair<int, int> a1 = { n, -1 };
        for (int i = i1 + 1; i < n; i++) {
            if (a[i] > q) a1 = min(a1, { a[i], i });
        }
        if (a1.second >= 0) {
            swap(a[i1], a[a1.second]);
            sort(a.begin() + i1 + 1, a.end());
            f();

        } else {
            for (int i = i1 - 1; i >= 0; i--) {
                if (a[i] < a[i + 1]) {
                    int i0 = i;
                    pair<int, int> a1 = { n, -1 };
                    for (int i = i0 + 1; i < n; i++) {
                        if (a[i] > a[i0] && a[i] != q) a1 = min(a1, { a[i], i });
                    }
                    if (a1.second < 0) continue;
                    swap(a[i0], a[a1.second]);
                    sort(a.begin() + i0 + 1, a.end());
                    f();
                }
            }
        }
    }

    cout << -1 << endl;

    return 0;
}
0