結果

問題 No.1051 PQ Permutation
ユーザー stoqstoq
提出日時 2020-03-29 06:58:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,456 bytes
コンパイル時間 2,145 ms
コンパイル使用メモリ 172,540 KB
実行使用メモリ 4,924 KB
最終ジャッジ日時 2023-09-19 07:28:55
合計ジャッジ時間 6,828 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,384 KB
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 3 ms
4,376 KB
testcase_27 WA -
testcase_28 AC 7 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 20 ms
4,700 KB
testcase_36 WA -
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 1 ms
4,384 KB
testcase_44 AC 2 ms
4,384 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/string:47,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/locale_classes.h:40,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/ios_base.h:41,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ios:42,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/istream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/sstream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/complex:45,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ccomplex:39,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:54,
         次から読み込み:  main.cpp:1:
メンバ関数 ‘__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(difference_type) const [with _Iterator = int*; _Container = std::vector<int>]’ 内,
    inlined from ‘int main()’ at main.cpp:152:64:
/usr/local/gcc7/include/c++/12.2.0/bits/stl_iterator.h:1144:47: 警告: ‘q_pos’ may be used uninitialized [-Wmaybe-uninitialized]
 1144 |       { return __normal_iterator(_M_current + __n); }
      |                                               ^~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:144:18: 備考: ‘q_pos’ はここで定義されています
  144 |       int p_pos, q_pos;
      |                  ^~~~~
メンバ関数 ‘__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(difference_type) const [with _Iterator = int*; _Container = std::vector<int>]’ 内,
    inlined from ‘int main()’ at main.cpp:152:13:
/usr/local/gcc7/include/c++/12.2.0/bits/stl_iterator.h:1144:47: 警告: ‘p_pos’ may be used uninitialized [-Wmaybe-uninitialized]
 1144 |       { return __normal_iterator(_M_current + __n); }
      |    

ソースコード

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];

  int Max1[100010], Max2[100010];
  Max1[n] = -1, Max2[n] = -2;
  for (int i = n; i > 0; i--)
  {
    int t = a[i - 1];
    if (t > Max1[i])
    {
      Max1[i - 1] = t;
      Max2[i - 1] = Max1[i];
    }
    else if (t > Max2[i])
    {
      Max1[i - 1] = Max1[i];
      Max2[i - 1] = t;
    }
    else
    {
      Max1[i - 1] = Max1[i];
      Max2[i - 1] = Max2[i];
    }
  }

  //条件を満たす順列Bで、先頭i文字が一致し、a[i]!=b[i]となるものがあるか
  int match = -1;
  bool used_p = false;
  rep(i, n)
  {
    if (used_p)
    {
      if (Max1[i] > a[i])
        match = i;
    }
    else
    {
      if ((Max1[i] > a[i] && Max1[i] != q) || Max2[i] > a[i])
        match = i;
    }
    if (a[i] == p)
      used_p = true;
    else if (a[i] == q && !used_p)
      break;
  }

  dbg(match);
  if (match == -1)
  {
    cout << -1 << endl;
    return 0;
  }
  vector<int> b(n);
  rep(i, n) b[i] = a[i];
  used_p = false;
  rep(i, match)
  {
    if (a[i] == p)
    {
      used_p = true;
      break;
    }
  }
  if (used_p)
  {
    cerr << 1 << endl;
    rep(i, n) b[i] = a[i];
    int m = INF, mi;
    REP(i, match + 1, n)
    {
      if (a[match] < a[i] && a[i] < m)
      {
        m = min(m, a[i]);
        mi = i;
      }
    }
    swap(b[match], b[mi]);
    sort(b.begin() + match + 1, b.end());
  }
  else
  {
    cerr << 2 << endl;
    int m = INF, mi;
    REP(i, match + 1, n)
    {
      if (a[match] < a[i] && a[i] != q && a[i] < m)
      {
        m = min(m, a[i]);
        mi = i;
      }
    }

    swap(b[match], b[mi]);
    sort(b.begin() + match + 1, b.end());

    if (p > q && m != p)
    {
      cerr << "2+" << endl;
      swap(b[match], b[mi]);
      sort(b.begin() + match + 1, b.end());
      int p_pos, q_pos;
      REP(i, match + 1, n)
      {
        if (b[i] == p)
          p_pos = i;
        else if (b[i] == q)
          q_pos = i;
      }
      rotate(b.begin() + p_pos, b.begin() + q_pos, b.begin() + q_pos + 1);
    }
  }

  rep(i, n) cout << b[i] << (i == n - 1 ? "\n" : " ");
  assert(b.size() == n);
  return 0;
}
0