結果
問題 |
No.1051 PQ Permutation
|
ユーザー |
![]() |
提出日時 | 2020-05-08 21:51:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,047 bytes |
コンパイル時間 | 454 ms |
コンパイル使用メモリ | 46,520 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 19:33:14 |
合計ジャッジ時間 | 3,157 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 WA * 20 |
ソースコード
#include <stdio.h> #include <algorithm> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int l = ri() - 1; int r = ri() - 1; int a[n]; for (auto &i : a) i = ri() - 1; if (!std::next_permutation(a, a + n)) { puts("-1"); return 0; } int max[n]; max[n - 1] = a[n - 1]; for (int i = n; --i; ) max[i - 1] = std::max(max[i], a[i - 1]); int lpos = std::find(a, a + n, l) - a; int rpos = std::find(a, a + n, r) - a; if (lpos > rpos) { int i = rpos; while (i >= 0 && max[i] == a[i]) i--; if (i == -1) { puts("-1"); return 0; } int min = 1000000000, index = -1; for (int j = i + 1; j < n; j++) if (a[j] > a[i] && min > a[j]) min = a[j], index = j; std::swap(a[i], a[index]); std::sort(a + i + 1, a + n); bool l_found = false; bool r_bury = false; for (auto j : a) { if (j == l) { l_found = true; printf("%d ", l + 1); if (r_bury) printf("%d ", r + 1); } else if (j == r && !l_found) r_bury = true; else printf("%d ", j + 1); } puts(""); } return 0; }