結果

問題 No.1051 PQ Permutation
ユーザー betrue12betrue12
提出日時 2020-05-09 02:26:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 4,129 ms
コンパイル使用メモリ 211,764 KB
実行使用メモリ 14,296 KB
最終ジャッジ日時 2023-09-19 08:16:02
合計ジャッジ時間 7,563 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 137 ms
14,176 KB
testcase_16 AC 130 ms
13,528 KB
testcase_17 AC 72 ms
4,920 KB
testcase_18 AC 72 ms
4,916 KB
testcase_19 AC 71 ms
4,916 KB
testcase_20 AC 108 ms
10,428 KB
testcase_21 AC 110 ms
10,856 KB
testcase_22 AC 72 ms
4,900 KB
testcase_23 AC 108 ms
10,468 KB
testcase_24 AC 107 ms
10,248 KB
testcase_25 AC 8 ms
4,380 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 11 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 10 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 10 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 59 ms
8,796 KB
testcase_36 AC 59 ms
8,796 KB
testcase_37 AC 56 ms
4,376 KB
testcase_38 AC 124 ms
14,296 KB
testcase_39 AC 105 ms
13,488 KB
testcase_40 AC 70 ms
4,920 KB
testcase_41 AC 94 ms
9,648 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 210 ms
13,452 KB
権限があれば一括ダウンロードができます

ソースコード

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<>());

    set<int> avail;
    for(int i=posQ+1; i<N; i++) avail.insert(A[i]);
    while(true){
        int pt = posQ;
        while(pt >= 0 && A[pt] > A[pt+1]){
            avail.insert(A[pt]);
            pt--;
        }
        if(pt == -1) fail();
        if(*avail.lower_bound(A[pt]) == Q){
            swap(A[pt], A[posQ]);
            avail.erase(Q);
            avail.insert(A[posQ]);
            posQ = pt;
        }else{
            break;
        }
    }

    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