結果

問題 No.1051 PQ Permutation
ユーザー 👑 hitonanodehitonanode
提出日時 2020-05-08 23:08:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 2,022 bytes
コンパイル時間 3,591 ms
コンパイル使用メモリ 207,488 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2023-09-19 08:02:16
合計ジャッジ時間 6,847 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 177 ms
13,272 KB
testcase_16 AC 149 ms
12,796 KB
testcase_17 AC 33 ms
5,072 KB
testcase_18 AC 33 ms
4,872 KB
testcase_19 AC 33 ms
4,964 KB
testcase_20 AC 95 ms
9,768 KB
testcase_21 AC 101 ms
10,048 KB
testcase_22 AC 34 ms
4,916 KB
testcase_23 AC 91 ms
9,996 KB
testcase_24 AC 92 ms
9,524 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 8 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 49 ms
8,476 KB
testcase_36 AC 50 ms
8,452 KB
testcase_37 AC 77 ms
12,992 KB
testcase_38 AC 107 ms
13,760 KB
testcase_39 AC 75 ms
13,240 KB
testcase_40 AC 65 ms
8,820 KB
testcase_41 AC 64 ms
8,892 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 112 ms
13,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

void output(vector<int> v)
{
    for (auto x : v) cout << x << ' ';
    cout << '\n';
    exit(0);
}

int in()
{
    int x;
    cin >> x;
    return x;
}

int main()
{
    const int N = in(), P = in(), Q = in();
    vector<int> A(N);
    cin >> A;
    int ip = 0, iq = 0;
    while (A[ip] != P) ip++;
    while (A[iq] != Q) iq++;
    bool fok = (ip < iq);
    set<int> s;

    bool fp = false, fq = false;
    if (P > Q and A[N - 2] == Q and A[N - 1] == P)
    {
        A[N - 2] = P, A[N - 1] = Q;
        output(A);
    }
    IREP(i, N)
    {
        if (A[i] == P) fp = true;
        if (A[i] == Q) fq = true;
        s.insert(A[i]);

        if ((fok or fq) and *prev(s.end()) > A[i])
        {
            auto itr = s.upper_bound(A[i]);
            if (fp and *itr == Q) itr = next(itr);
            if (itr == s.end()) continue;
            int x = *itr;
            s.erase(itr);

            A[i] = x;
            s.erase(x);
            if (x == P) fp = false;
            FOR(j, i + 1, N)
            {
                auto itr = s.begin();
                if (fp and *itr == Q) itr = next(itr);
                A[j] = *itr;
                s.erase(A[j]);
                if (A[j] == P) fp = false;
            }
            output(A);
        }
    }
    puts("-1");
}
0