結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 1 ms
4,380 KB
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 2 ms
5,420 KB
testcase_10 RE -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 35 ms
6,544 KB
testcase_18 AC 37 ms
6,500 KB
testcase_19 AC 36 ms
6,472 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 35 ms
6,520 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 4 ms
4,384 KB
testcase_27 RE -
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 4 ms
5,536 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
5,452 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 AC 120 ms
15,876 KB
testcase_39 RE -
testcase_40 WA -
testcase_41 AC 73 ms
11,168 KB
testcase_42 RE -
testcase_43 AC 2 ms
4,388 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 AC 2 ms
5,256 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:32:13: 警告: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |     if(pos[P] < pos[Q]){
      |        ~~~~~^
main.cpp:24:8: 備考: ‘pos’ はここで宣言されています
   24 |     ll pos[200010];
      |        ^~~
main.cpp:32:22: 警告: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |     if(pos[P] < pos[Q]){
      |                 ~~~~~^
main.cpp:24:8: 備考: ‘pos’ はここで宣言されています
   24 |     ll pos[200010];
      |        ^~~
main.cpp:32:13: 警告: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |     if(pos[P] < pos[Q]){
      |        ~~~~~^
main.cpp:24:8: 備考: ‘pos’ はここで宣言されています
   24 |     ll pos[200010];
      |        ^~~
main.cpp:32:22: 警告: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |     if(pos[P] < pos[Q]){
      |                 ~~~~~^
main.cpp:24:8: 備考: ‘pos’ はここで宣言されています
   24 |     ll pos[200010];
      |        ^~~

ソースコード

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{
        exit(1);
        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()){
                    st.insert(A[i]);
                }else{
                    ok = true;
                    ll v = *itr;
                    A[i] = v;
                    st.erase(itr);
                    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