結果

問題 No.1051 PQ Permutation
ユーザー stoqstoq
提出日時 2020-03-26 20:13:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,543 bytes
コンパイル時間 1,760 ms
コンパイル使用メモリ 167,832 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-19 07:27:53
合計ジャッジ時間 6,674 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,384 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 5 ms
4,380 KB
testcase_26 AC 4 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 3 ms
4,384 KB
testcase_32 AC 4 ms
4,384 KB
testcase_33 AC 3 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 18 ms
4,380 KB
testcase_36 AC 18 ms
4,384 KB
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,380 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;
  int a[100010];
  rep(i, n) cin >> a[i];

  //先頭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;
}
0