結果

問題 No.1051 PQ Permutation
ユーザー 👑 hitonanodehitonanode
提出日時 2020-05-08 23:06:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,905 bytes
コンパイル時間 3,682 ms
コンパイル使用メモリ 208,248 KB
実行使用メモリ 13,732 KB
最終ジャッジ日時 2023-09-19 08:02:05
合計ジャッジ時間 6,879 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 169 ms
13,420 KB
testcase_16 AC 151 ms
12,628 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 98 ms
9,720 KB
testcase_21 AC 98 ms
10,292 KB
testcase_22 WA -
testcase_23 AC 93 ms
9,676 KB
testcase_24 AC 90 ms
9,584 KB
testcase_25 AC 6 ms
4,380 KB
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 8 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 8 ms
4,380 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 50 ms
8,416 KB
testcase_36 AC 50 ms
8,452 KB
testcase_37 AC 77 ms
13,368 KB
testcase_38 AC 107 ms
13,732 KB
testcase_39 AC 75 ms
13,300 KB
testcase_40 AC 65 ms
8,872 KB
testcase_41 AC 64 ms
8,872 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 111 ms
12,992 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;
    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 (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