結果
| 問題 |
No.1051 PQ Permutation
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-05-18 23:02:59 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,628 bytes |
| コンパイル時間 | 1,338 ms |
| コンパイル使用メモリ | 125,344 KB |
| 最終ジャッジ日時 | 2025-01-21 13:45:36 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 WA * 20 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n, p, q; cin >> n >> p >> q;
p--; q--;
vector<int> a(n), pos(n);
for(int i = 0; i < n; i++) {
cin >> a[i]; a[i]--;
}
next_permutation(a.begin(), a.end());
for(int i = 0; i < n; i++) pos[a[i]] = i;
if(pos[p] < pos[q]){
for(int x : a) cout << x << ' ';
cout << endl;
return 0;
}
set<int> st;
for(int i = n-1; i > pos[q]; i--) st.insert(a[i]);
for(int i = pos[q]; i >= 0; i--){
st.insert(a[i]);
auto ok = [&]() -> bool{
auto p = st.end(); p--;
if((*p) == a[i]) return false;
if(st.size() == 1) return false;
if((*p) != q) return true;
p--;
if((*p) == a[i]) return false;
return true;
};
if(ok()){
vector<int> v;
for(int j = n-1; j >= i; j--) v.push_back(a[j]);
sort(v.begin(), v.end());
int idx = -1;
for(int j = 0; j < v.size(); j++) {
if(v[j] == a[i]) idx = j;
}
int nx = [&]() -> int {
if(v[idx+1] != q) return v[idx+1];
return v[idx+2];
}();
for(int j = 0; j < i; j++) cout << a[j]+1 << ' ';
cout << nx+1 << ' ';
if(p < q || p == nx){
for(int x : v){
if(x != nx) cout << x+1 << ' ';
}
cout << endl;
return 0;
}
bool f = false;
for(int x : v){
if(x == p || x == q || x == nx) continue;
if(x > p && !f){
f = true;
cout << p+1 << ' ' << q+1 << ' ';
}
cout << x+1 << ' ';
}
cout << endl;
return 0;
}
}
cout << -1 << endl;
}
milanis48663220