結果

問題 No.1051 PQ Permutation
ユーザー stoqstoq
提出日時 2020-03-31 04:16:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,835 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 173,896 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 19:30:38
合計ジャッジ時間 7,372 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 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 9 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 13 ms
5,376 KB
testcase_29 AC 5 ms
5,376 KB
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 9 ms
5,376 KB
testcase_33 AC 4 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 42 ms
5,376 KB
testcase_36 AC 43 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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:47,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In member function '__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:95:43:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_iterator.h:1144:47: warning: 'q_pos' may be used uninitialized [-Wmaybe-uninitialized]
 1144 |       { return __normal_iterator(_M_current + __n); }
      |                                               ^~~
main.cpp: In function 'int main()':
main.cpp:87:16: note: 'q_pos' was declared here
   87 |     int p_pos, q_pos;
      |                ^~~~~
main.cpp:87:9: warning: 'p_pos' may be used uninitialized [-Wmaybe-uninitialized]
   87 |     int p_pos, q_pos;
      |         ^~~~~
main.cpp:82:22: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   82 |   swap(b[match], b[mi]);
      |                      ^
main.cpp:73:16: note: 'mi' was declared here
   73 |   int m = INF, mi;
      |      

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

int main()
{
  int n, p, q;
  cin >> n >> p >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    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;
  for (int i = 0; i < n; i++)
  {
    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;
  }

  if (match == -1)
  {
    cout << -1 << endl;
    return 0;
  }

  vector<int> b = a;
  used_p = false;
  for (int i = 0; i < match; i++)
  {
    if (a[i] == p)
    {
      used_p = true;
      break;
    }
  }

  int m = INF, mi;
  for (int i = match + 1; i < n; i++)
  {
    if (a[match] < a[i] && a[i] < m && (used_p || a[i] != q))
    {
      m = min(m, a[i]);
      mi = i;
    }
  }
  swap(b[match], b[mi]);
  sort(b.begin() + match + 1, b.end());

  if (!used_p && p > q && m != p)
  {
    int p_pos, q_pos;
    for (int i = match + 1; i < n; i++)
    {
      if (b[i] == p)
        p_pos = i;
      else if (b[i] == q)
        q_pos = i;
    }
    rotate(b.begin() + q_pos, b.begin() + q_pos + 1, b.begin() + p_pos + 1);
  }

  for (int i = 0; i < n; i++)
    cout << b[i] << (i == n - 1 ? "\n" : " ");
  return 0;
}
0