結果

問題 No.1051 PQ Permutation
ユーザー betrue12betrue12
提出日時 2020-05-08 21:55:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 904 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 206,128 KB
実行使用メモリ 5,224 KB
最終ジャッジ日時 2023-09-19 07:33:46
合計ジャッジ時間 6,857 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 86 ms
4,900 KB
testcase_16 AC 84 ms
4,960 KB
testcase_17 AC 72 ms
5,020 KB
testcase_18 AC 72 ms
4,956 KB
testcase_19 AC 72 ms
4,904 KB
testcase_20 AC 80 ms
5,224 KB
testcase_21 AC 80 ms
4,968 KB
testcase_22 AC 72 ms
4,984 KB
testcase_23 AC 79 ms
4,888 KB
testcase_24 AC 80 ms
5,104 KB
testcase_25 AC 8 ms
4,376 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 11 ms
4,376 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 36 ms
4,376 KB
testcase_36 AC 35 ms
4,392 KB
testcase_37 AC 56 ms
4,376 KB
testcase_38 AC 73 ms
5,096 KB
testcase_39 AC 56 ms
4,380 KB
testcase_40 AC 70 ms
4,900 KB
testcase_41 AC 72 ms
4,892 KB
testcase_42 WA -
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

void fail(){
    cout << -1 << endl;
    exit(0);
}

void answer(vector<int> A){
    int N = A.size();
    for(int i=0; i<N; i++) cout << A[i]+1 << " \n"[i==N-1];
    exit(0);
}

int main(){
    int N, P, Q;
    cin >> N >> P >> Q;
    P--; Q--;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i], A[i]--;
    if(!next_permutation(A.begin(), A.end())) fail();
    int posP = -1, posQ = -1;
    for(int i=0; i<N; i++){
        if(A[i] == P) posP = i;
        if(A[i] == Q) posQ = i;
    }
    if(posP < posQ) answer(A);

    sort(A.begin()+posQ+1, A.end(), greater<>());
    if(!next_permutation(A.begin(), A.end())) fail();
    for(int i=0; i<N; i++){
        if(A[i] == P) posP = i;
        if(A[i] == Q) posQ = i;
    }
    if(posP < posQ) answer(A);

    A.erase(A.begin()+posQ);
    A.insert(A.begin()+posP, Q);
    answer(A);
    return 0;
}
0