結果

問題 No.1051 PQ Permutation
ユーザー yuji9511yuji9511
提出日時 2020-05-09 00:36:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,669 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 171,452 KB
実行使用メモリ 15,992 KB
最終ジャッジ日時 2023-09-19 08:14:41
合計ジャッジ時間 6,935 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 150 ms
15,768 KB
testcase_16 AC 142 ms
15,052 KB
testcase_17 AC 37 ms
6,548 KB
testcase_18 AC 36 ms
6,548 KB
testcase_19 AC 36 ms
6,560 KB
testcase_20 AC 95 ms
12,116 KB
testcase_21 AC 96 ms
12,568 KB
testcase_22 AC 36 ms
6,608 KB
testcase_23 AC 89 ms
12,104 KB
testcase_24 AC 88 ms
11,992 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 7 ms
4,380 KB
testcase_29 AC 4 ms
5,548 KB
testcase_30 AC 7 ms
4,444 KB
testcase_31 AC 3 ms
5,612 KB
testcase_32 AC 8 ms
4,440 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 49 ms
10,464 KB
testcase_36 AC 51 ms
9,932 KB
testcase_37 AC 108 ms
15,992 KB
testcase_38 AC 118 ms
15,928 KB
testcase_39 AC 90 ms
15,868 KB
testcase_40 AC 71 ms
11,224 KB
testcase_41 AC 72 ms
11,236 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
5,388 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
5,396 KB
testcase_48 AC 109 ms
15,976 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 pos[200010];
    rep(i,0,N) pos[A[i]] = i;
    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;
                ll v = *itr;
                st.erase(v);
                st.insert(A[i]);
                if(v == P) st.insert(Q);
                A[i] = v;
                ll idx = i+1;
                while(st.size()){
                    auto it = st.begin();
                    ll val = *it;
                    if(val == P) st.insert(Q);
                    A[idx] = val;
                    idx++;
                    st.erase(it);
                }
                break;
            }else{
                st.insert(A[i]);
            }

        }
    }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 && v != P) st.insert(A[i]);
                    if(v == P){
                        st.insert(A[i]);
                    }else 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;
                        // print(val);
                        if(val == P) st.insert(Q);
                        A[idx] = val;
                        idx++;
                        st.erase(val);

                    }
                    break;
                }

            }
        }

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

}
0