結果
| 問題 | No.1051 PQ Permutation | 
| コンテスト | |
| ユーザー |  merom686 | 
| 提出日時 | 2020-05-09 00:56:22 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 57 ms / 2,000 ms | 
| コード長 | 2,353 bytes | 
| コンパイル時間 | 1,460 ms | 
| コンパイル使用メモリ | 98,872 KB | 
| 最終ジャッジ日時 | 2025-01-10 09:28:45 | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 46 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:48:25: warning: ‘i1’ may be used uninitialized [-Wmaybe-uninitialized]
   48 |         for (int i = i1 + 1; i < n; i++) {
      |                      ~~~^~~
main.cpp:37:18: note: ‘i1’ was declared here
   37 |     int i0 = -1, i1;
      |                  ^~
            
            ソースコード
#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;
}
            
            
            
        