結果
問題 | No.1051 PQ Permutation |
ユーザー |
|
提出日時 | 2020-05-10 00:59:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 56 ms / 2,000 ms |
コード長 | 2,812 bytes |
コンパイル時間 | 1,590 ms |
コンパイル使用メモリ | 175,956 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-06 11:07:42 |
合計ジャッジ時間 | 4,711 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr char newl = '\n'; bool judge(const vector<int>& a, int p, int q) { for (int x : a) { if (x == q) return false; if (x == p) return true; } assert(false); } bool judge2(const vector<int>& a, int q, int& hoge) { int n = a.size(); hoge = n + 1; bool res = true; for (int i = n - 1; i >= 0; i--) { if (a[i] == q) return res; if (a[i] > q) { res = false; hoge = min(hoge, a[i]); } } assert(false); } void func1(vector<int>& a, int q, int hoge) { int n = a.size(); int idx = -1; for (int i = n - 1; i >= 0; --i) { if (a[i] == q) { a[i] = hoge; idx = i; } else if (a[i] == hoge) a[i] = q; } sort(a.begin() + idx + 1, a.end()); } void func2(vector<int>& a, int p, int q) { int n = a.size(); for (int i = 0; i < n; i++) { if (a[i] == q) { assert(i + 1 < n); swap(a[i], a[i + 1]); if (a[i] == p) return; } } } int func3(const vector<int>& a, int q) { int n = a.size(); int idx = -1; for (int i = 0; i < n; ++i) { if (a[i] == q) return idx; if (a[i] < q) idx = i; } return idx; } void print(const vector<int>& a) { int n = a.size(); for (int i = 0; i < n; i++) { cout << a[i] << " \n"[i + 1 == n]; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, p, q; cin >> n >> p >> q; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> aa = a; sort(aa.begin(), aa.end(), greater<>()); if (a == aa) { cout << -1 << newl; return 0; } next_permutation(a.begin(), a.end()); if (judge(a, p, q)) { print(a); return 0; } int hoge; int foo = 0; if (!judge2(a, q, hoge)) { func1(a, q, hoge); if (judge(a, p, q)) { print(a); return 0; } func2(a, p, q); if (judge(a, p, q)) { print(a); return 0; } else { cout << -1 << newl; return 0; } } else { hoge: int idx = func3(a, q); if (idx == -1) { cout << -1 << newl; return 0; } if (!judge2(a, a[idx], hoge)) { func1(a, a[idx], hoge); if (judge(a, p, q)) { print(a); return 0; } else { if (foo++ < 2) goto hoge; cout << -1 << newl; return 0; } } else { cout << -1 << newl; return 0; } } return 0; }