結果

問題 No.1051 PQ Permutation
ユーザー milanis48663220milanis48663220
提出日時 2021-05-18 23:11:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 1,385 ms
コンパイル使用メモリ 127,724 KB
実行使用メモリ 15,392 KB
最終ジャッジ日時 2024-04-17 08:12:07
合計ジャッジ時間 5,137 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 141 ms
15,244 KB
testcase_16 AC 138 ms
14,528 KB
testcase_17 AC 37 ms
5,376 KB
testcase_18 AC 37 ms
5,376 KB
testcase_19 AC 37 ms
5,376 KB
testcase_20 AC 92 ms
10,944 KB
testcase_21 AC 95 ms
11,392 KB
testcase_22 AC 36 ms
5,376 KB
testcase_23 AC 90 ms
10,880 KB
testcase_24 AC 90 ms
10,752 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 7 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 9 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 53 ms
9,216 KB
testcase_36 AC 44 ms
9,408 KB
testcase_37 AC 22 ms
5,376 KB
testcase_38 AC 93 ms
15,392 KB
testcase_39 AC 76 ms
14,080 KB
testcase_40 AC 34 ms
5,376 KB
testcase_41 AC 61 ms
10,192 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 80 ms
14,208 KB
権限があれば一括ダウンロードができます

ソースコード

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]--;
    }
    if(!next_permutation(a.begin(), a.end())){
        cout << -1 << endl;
        return 0;
    }
    for(int i = 0; i < n; i++) pos[a[i]] = i;
    if(pos[p] < pos[q]){
        for(int x : a) cout << x+1 << ' ';
        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 || p == nx){
                for(int x : v){
                    if(x != nx) cout << x+1 << ' ';
                }
                cout << endl;
                return 0;
            }
            bool f = false;
            for(int x : v){
                if(x == p || x == q || x == nx) continue;
                if(x > p && !f){
                    f = true;
                    cout << p+1 << ' ' << q+1 << ' ';
                }
                cout << x+1 << ' ';
            }
            if(!f) cout << p+1 << ' ' << q+1 << ' ';
            cout << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
0