結果
| 問題 |
No.1051 PQ Permutation
|
| コンテスト | |
| ユーザー |
stoq
|
| 提出日時 | 2020-03-26 21:22:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,763 bytes |
| コンパイル時間 | 1,774 ms |
| コンパイル使用メモリ | 170,812 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-05 19:29:03 |
| 合計ジャッジ時間 | 5,404 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 WA * 3 RE * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;
const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ld PI = acos(-1.0);
const ld EPS = 1e-11;
#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(30) << setiosflags(ios::fixed);
int n, p, q;
cin >> n >> p >> q;
int a[100010];
rep(i, n) cin >> a[i];
assert(2 <= n && n <= 100000);
assert(1 <= p && p <= n);
assert(1 <= q && q <= n);
assert(p != q);
int cnt[100010] = {};
rep(i, n) cnt[a[i]]++;
for (int i = 1; i <= n; ++i)
{
assert(cnt[i] == 1);
}
//先頭x文字がAと一致
auto simulate = [&](int x) {
if (x == -1)
return true;
bool used[100010] = {};
rep(i, x)
{
if (a[i] == q && !used[p])
return false;
used[a[i]] = true;
}
if (used[p])
{
REP(i, x, n)
{
if (a[i] > a[x])
return true;
}
return false;
}
else
{
REP(i, x, n)
{
if (a[i] > a[x] && a[i] != q)
return true;
}
return false;
}
};
int lo = -2, hi = n;
while (hi - lo > 1)
{
int mi = (lo + hi) / 2;
if (simulate(mi))
lo = mi;
else
hi = mi;
}
int match = lo;
dbg(match);
if (match == -1)
{
cout << -1 << endl;
return 0;
}
bool used[100010] = {};
int b[100010];
rep(i, match)
{
used[a[i]] = true;
b[i] = a[i];
}
if (used[p])
{
for (int i = a[match] + 1; i <= n; i++)
{
if (!used[i])
{
b[match] = i;
used[i] = true;
break;
}
}
int bi = match + 1;
for (int i = 1; i <= n && bi < n; i++)
{
if (!used[i])
{
b[bi] = i;
bi++;
}
}
assert(bi == n);
}
else if (p < q)
{
for (int i = a[match] + 1; i <= n; i++)
{
if (!used[i] && i != q)
{
b[match] = i;
used[i] = true;
break;
}
}
int bi = match + 1;
for (int i = 1; i <= n && bi < n; i++)
{
if (!used[i])
{
b[bi] = i;
bi++;
}
}
assert(bi == n);
}
else
{
int bi = match;
for (int i = a[match] + 1; i <= n; i++)
{
if (!used[i] && i != q)
{
b[bi] = i;
used[i] = true;
bi++;
if (i == p)
{
b[bi] = q;
used[q] = true;
bi++;
}
break;
}
}
for (int i = 1; i <= n; i++)
{
if (!used[i] && i != q)
{
b[bi] = i;
bi++;
if (i == p)
{
b[bi] = q;
bi++;
}
}
}
assert(bi == n);
}
rep(i, n) cout << b[i] << (i == n - 1 ? "\n" : " ");
return 0;
}
stoq