結果

問題 No.1051 PQ Permutation
ユーザー yuji9511yuji9511
提出日時 2020-05-08 23:18:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,729 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 171,792 KB
実行使用メモリ 15,956 KB
最終ジャッジ日時 2023-09-19 08:07:30
合計ジャッジ時間 5,881 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,388 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,428 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
5,340 KB
testcase_13 AC 2 ms
5,336 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 36 ms
6,500 KB
testcase_18 AC 36 ms
6,524 KB
testcase_19 AC 36 ms
6,692 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 36 ms
6,420 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 3 ms
4,380 KB
testcase_27 WA -
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 3 ms
5,492 KB
testcase_34 AC 2 ms
5,480 KB
testcase_35 AC 49 ms
10,460 KB
testcase_36 AC 51 ms
9,768 KB
testcase_37 AC 108 ms
15,904 KB
testcase_38 AC 119 ms
15,876 KB
testcase_39 AC 92 ms
15,956 KB
testcase_40 WA -
testcase_41 AC 72 ms
11,228 KB
testcase_42 WA -
testcase_43 AC 2 ms
5,508 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 2 ms
5,344 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 109 ms
15,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,P,Q;
    cin >> N >> P >> Q;
    ll A[200010];
    rep(i,0,N) cin >> A[i];
    bool ok = false;
    ll p = -1, q = -1;
    ll pos[200010];
    rep(i,0,N) pos[A[i]] = i;
    // ll r = next_permutation(A, A+N);
    // if(r == 0){
    //     print(-1);
    //     return 0;
    // }
    set<ll> st;
    if(pos[P] < pos[Q]){
        rrep(i,N-1,0){
            ll cur = A[i];
            if(cur == P) st.erase(Q);
            auto itr = st.lower_bound(cur);
            if(itr != st.end()){
                ok = true;
                // print(i);
                ll v = *itr;
                st.erase(v);
                st.insert(A[i]);
                if(A[i] == P) st.erase(Q);
                A[i] = v;
                ll idx = i+1;
                // print(idx);
                while(st.size()){
                    auto it = st.begin();
                    ll val = *it;
                    // print(val);
                    if(val == P) st.insert(Q);
                    A[idx] = val;
                    idx++;
                    st.erase(it);
                }
                break;
            }else{
                st.insert(A[i]);
                if(A[i] == P){
                    st.erase(Q);
                }
            }

        }
    }else{
        rrep(i,N-1,0){
            ll cur = A[i];
            if(i > pos[Q]){
                st.insert(A[i]);
            }else{
                auto itr = st.lower_bound(A[i]);
                if(itr == st.end()){
                    if(A[i] != Q) st.insert(A[i]);
                }else{
                    ok = true;
                    ll v = *itr;
                    if(A[i] != Q) st.insert(A[i]);
                    A[i] = v;
                    st.erase(v);
                    ll idx = i+1;

                    while(st.size()){
                        auto itr = st.begin();
                        ll val = *itr;
                        if(val == P) st.insert(Q);
                        A[idx] = val;
                        idx++;
                        st.erase(val);

                    }
                }

            }
        }

    }
    if(not ok){
        print(-1);
    }else{
        printa(A, N);
    }
    

}
0