結果

問題 No.1051 PQ Permutation
ユーザー milanis48663220milanis48663220
提出日時 2021-05-18 22:55:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,644 bytes
コンパイル時間 1,345 ms
コンパイル使用メモリ 124,552 KB
最終ジャッジ日時 2025-01-21 13:45:28
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, p, q; cin >> n >> p >> q;
    p--; q--;
    vector<int> a(n), pos(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i]; a[i]--;
    }
    next_permutation(a.begin(), a.end());
    for(int i = 0; i < n; i++) pos[a[i]] = i;
    if(pos[p] < pos[q]){
        for(int x : a) cout << x << ' ';
        cout << endl;
        return 0;
    }
    set<int> st;
    for(int i = n-1; i > pos[q]; i--) st.insert(a[i]);
    for(int i = pos[q]; i >= 0; i--){
        st.insert(a[i]);
        auto ok = [&]() -> bool{
            auto p = st.end(); p--;
            if((*p) == a[i]) return false;
            if(st.size() == 1) return false;
            if((*p) != q) return true;
            p--;
            if((*p) == a[i]) return false;
            return true;
        };
        
        if(ok()){
            vector<int> v;
            for(int j = n-1; j >= i; j--) v.push_back(a[j]);
            sort(v.begin(), v.end());
            int idx = -1;
            for(int j = 0; j < v.size(); j++) {
                if(v[j] == a[i]) idx = j;
            }
            int nx = [&]() -> int {
                if(v[idx+1] != q) return v[idx+1];
                return v[idx+2];
            }();
            for(int j = 0; j < i; j++) cout << a[j]+1 << ' ';
            cout << nx+1 << ' ';
            if(p < q){
                for(int x : v){
                    if(x != nx) cout << x+1 << ' ';
                }
                cout << endl;
                return 0;
            }
            bool f = false;
            for(int x : v){
                if(x == nx) continue;
                if(x == p || x == q) continue;
                if(x > p && !f){
                    f = true;
                    cout << p+1 << ' ' << q+1 << ' ';
                }
                cout << x+1 << ' ';
            }
            cout << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
0