結果

問題 No.1051 PQ Permutation
ユーザー tarattata1tarattata1
提出日時 2020-05-08 23:22:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,147 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 88,316 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2023-09-19 08:09:43
合計ジャッジ時間 6,209 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,488 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,388 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 171 ms
12,644 KB
testcase_16 AC 154 ms
11,908 KB
testcase_17 AC 43 ms
4,380 KB
testcase_18 AC 42 ms
4,380 KB
testcase_19 AC 44 ms
4,380 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <cassert>
//#include <numeric>
#pragma warning(disable:4996) 
 
typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;

bool solve0(int n, int p, int q, vector<int>& a)
{
    next_permutation(a.begin(), a.end());

    int flag=0, idp = -1, idq = -1;
    int i;
    for (i = 0; i < n; i++) {
        if (a[i] != i) flag = 1;
        if (a[i] == p) idp = i;
        if (a[i] == q) idq = i;
    }
    if (flag == 0) {
        return false;
    }
    if (idp < idq) {
        return true;
    }

    set<int> z;
    for (i = idq+1; i < n; i++) {
        z.insert(a[i]);
    }
    int curr = idq;
    while (1) {
        if (curr < 0) {
            return false;
        }
        auto it = z.rbegin();
        if (*it > idq) {
            break;
        }
        z.insert(a[curr]);
        curr--;
    }

    for (i = curr + 1; i < n; i++) {
        auto it=z.rbegin();
        a[i] = *it;
        z.erase(*it);
    }
    bool ok = solve0(n, p, q, a);
    return ok;
}

void solve()
{
    int n, p, q;
    scanf("%d%d%d", &n, &p, &q);  p--; q--;
    vector<int> a(n);
    int i;
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]); a[i]--;
    }
    bool ok = solve0(n, p, q, a);
    if (!ok) {
        printf("-1\n"); return;
    }
    for (i = 0; i < n; i++) {
        printf("%d", a[i]+1);
        if (i < n - 1) printf(" ");
        else printf("\n");
    }

    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}
0