結果
問題 | No.1051 PQ Permutation |
ユーザー |
|
提出日時 | 2020-05-08 21:55:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 904 bytes |
コンパイル時間 | 2,601 ms |
コンパイル使用メモリ | 201,528 KB |
最終ジャッジ日時 | 2025-01-10 08:37:46 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 WA * 3 |
ソースコード
#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;}