結果

問題 No.1051 PQ Permutation
ユーザー milanis48663220milanis48663220
提出日時 2021-05-18 23:02:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,628 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 127,872 KB
実行使用メモリ 15,396 KB
最終ジャッジ日時 2024-04-17 08:11:39
合計ジャッジ時間 4,603 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 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 WA -
testcase_13 AC 2 ms
5,376 KB
testcase_14 WA -
testcase_15 AC 121 ms
15,116 KB
testcase_16 AC 115 ms
14,528 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 81 ms
10,948 KB
testcase_21 AC 82 ms
11,392 KB
testcase_22 WA -
testcase_23 AC 78 ms
10,948 KB
testcase_24 AC 77 ms
10,768 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 WA -
testcase_27 AC 3 ms
5,376 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 48 ms
9,344 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 85 ms
15,396 KB
testcase_39 AC 71 ms
14,208 KB
testcase_40 WA -
testcase_41 AC 60 ms
9,984 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 79 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]--;
    }
    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 || 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 << ' ';
            }
            cout << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
0