結果

問題 No.1051 PQ Permutation
ユーザー petite_progpetite_prog
提出日時 2020-05-08 23:13:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,471 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 177,756 KB
実行使用メモリ 7,900 KB
最終ジャッジ日時 2023-09-19 08:04:50
合計ジャッジ時間 6,408 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,384 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 54 ms
6,280 KB
testcase_16 AC 52 ms
5,972 KB
testcase_17 AC 39 ms
6,172 KB
testcase_18 AC 39 ms
5,472 KB
testcase_19 RE -
testcase_20 AC 47 ms
5,400 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 47 ms
5,480 KB
testcase_24 AC 47 ms
5,564 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 6 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 WA -
testcase_36 AC 20 ms
4,476 KB
testcase_37 AC 24 ms
7,692 KB
testcase_38 RE -
testcase_39 AC 31 ms
7,300 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 2 ms
4,380 KB
testcase_47 WA -
testcase_48 AC 23 ms
7,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
   ∫ ∫ ∫
   ノヽ
  (_  )
 (_    )
(______ )
 ヽ(´・ω・)ノ 
   |  /
   UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
const char bn = '\n';
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
    os<<endl;
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, map<T,S> const&M){
    for(auto e:M){
        os<<e;
    }
    os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

bool solve1(vector<int> A,int P, int Q, int idx,vector<int>& ans){
    ans.clear();
    auto B=A;

    vector<int> head,tails;
    REP(i,A.size()){
        if(i<idx) head.emplace_back(A[i]);
        else tails.emplace_back(A[i]);
    }

    if(not next_permutation(ALL(head))) return false;
    bool flag = false;
    REP(i,head.size()){
        if(A[i]!=head[i]) flag = true;
        else if(flag){
            tails.emplace_back(A[i]);
        }
    }

    sort(ALL(tails));

    bool P_put = false, Q_wait = false;
    ans = head;
    for(auto a:tails){
        if(a==Q){
            if(P_put) ans.emplace_back(a);
            else Q_wait = true;
        } else{
            ans.emplace_back(a);
            if(P==a){
                if(Q_wait) ans.emplace_back(Q);
                P_put = true;
            }
        }
    }

    return true;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N,P,Q; cin >> N >> P >> Q;
    vector<int> A(N);
    REP(i,N) cin >> A[i];

    vector<int> ans;
    vector<bool> remain(N,true);
    bool is_P = false;
    int idx = 0;
    REP(i,N){
        if(A[i]==P){
            is_P = true;
            idx = i;
            break;
        }else if(A[i]==Q) {idx=i;break;}
        remain[A[i]] = false;
        ans.emplace_back(A[i]);
    }
    
    vector<int > A_tails;
    int Am = A[idx];
    for(int i=idx;i<N;i++) A_tails.emplace_back(A[i]);
    if(is_P){
        
        if(next_permutation(ALL(A_tails))){
            if(find(ALL(A_tails),P)-A_tails.begin() < find(ALL(A_tails),Q)-A_tails.begin()){
                //ok!!
                for(auto a:A_tails) ans.emplace_back(a);
                cout << ans;
                return 0;
            }
        }else{
            if(solve1(A,P,Q,idx,ans)){
                cout << ans;
            }else cout << -1 << bn;
            return 0;
        }
    }

    sort(ALL(A_tails));
    bool ok = false;
    for(auto a:A_tails){
        if(Am < a){
            ans.emplace_back(a);
            A_tails.erase(find(ALL(A_tails),a));
            ok = true;
            break;
        }
    }
    if(not ok){
        if(solve1(A,P,Q,idx,ans)){
            cout << ans;
        }else cout << -1 << bn;
        return 0;
    }
    bool P_put = false, Q_wait = false;
    for(auto a:A_tails){
        if(a==Q){
            if(P_put) ans.emplace_back(a);
            else Q_wait = true;
        } else{
            ans.emplace_back(a);
            if(P==a){
                if(Q_wait) ans.emplace_back(Q);
                P_put = true;
            }
        }
    }




    cout << ans;
}
0