結果
問題 | No.1051 PQ Permutation |
ユーザー |
|
提出日時 | 2020-05-09 18:02:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,121 bytes |
コンパイル時間 | 2,354 ms |
コンパイル使用メモリ | 199,208 KB |
最終ジャッジ日時 | 2025-01-10 09:58:35 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 WA * 3 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;#define rep(i, n) for (int i = 0; i < (n); i++)#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)#define range(a) a.begin(), a.end()int index(vector<int> a, int b) {return find(range(a), b) - a.begin();}void answer(vector<int> a) {rep(i, a.size()) cout << a[i] << " \n"[i == a.size() - 1];exit(0);}void fail() {cout << -1 << endl;exit(0);}int main() {int N, P, Q; cin >> N >> P >> Q;vector<int> A(N); rep(i, N) cin >> A[i];if (not next_permutation(range(A))) fail();int p = index(A, P);int q = index(A, Q);if (p < q) answer(A);if (P < Q) {sort(A.begin() + q + 1, A.end(), greater<>());if (not next_permutation(range(A))) fail();answer(A);} else {sort(A.begin() + q + 1, A.end(), greater<>());if (not next_permutation(range(A))) fail();p = index(A, P);q = index(A, Q);if (p < q) answer(A);while (q < p) {swap(A[q], A[q + 1]);q++;}answer(A);}}