結果

問題 No.1051 PQ Permutation
ユーザー stoqstoq
提出日時 2020-03-29 08:40:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,119 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 173,880 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 19:30:09
合計ジャッジ時間 6,392 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 6 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 5 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 19 ms
5,376 KB
testcase_36 AC 19 ms
5,376 KB
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
  vector<int> a(n);
  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] = {};
  vector<int> b(n);
  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" : " ");

  //----------debug----------
  assert(a < b);
  int p_pos, q_pos;
  rep(i, n)
  {
    if (b[i] == p)
      p_pos = i;
    else if (b[i] == q)
      q_pos = i;
  }
  assert(p_pos < q_pos);
  prev_permutation(all(b));
  rep(i, n)
  {
    if (b[i] == p)
      p_pos = i;
    else if (b[i] == q)
      q_pos = i;
  }
  assert(p_pos > q_pos || a == b);

  return 0;
}
0