QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168809#5047. PermutationabsintheWA 32ms19836kbC++202.8kb2023-09-08 22:32:442023-09-08 22:32:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-08 22:32:46]
  • 评测
  • 测评结果:WA
  • 用时:32ms
  • 内存:19836kb
  • [2023-09-08 22:32:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 500'005;

int a[N];
int loc[N];

int l[N], r[N];
int fl[N], fr[N];
int tag[N];
int mer[N];
int n, c;

ll MOD = 998244353;
ll fac[N];

void add_group(int l, int r, int g, int fl, int fr, bool init) {
    if(r <= 0 || l > n) return;
    if(init && l <= 0) {
        for(int i = 1; i <= r; ++i) tag[i] = -1;
        return;
    }
    if(init && r > n) {
        for(int i = l; i <= n; ++i) tag[i] = -1;
        return;
    }
    int tmp = 0;
    l = max(l, 1);
    r = min(r, n);
    for(int i = l; i <= r; ++i) {
        if(tag[i] == -1) {
            if(init) return;
            if(fl) r = min(r, i - 1);
            if(fr) l = max(l, i + 1);
        }
        // if(tag[i] != 0) tmp = tag[i];
    }
    if(l > r) return;

    for(int i = l; i <= r; ++i) if(tag[i] != 0) tmp = tag[i];

    if(::l[g] == 0)
        ::l[g] = ::r[g] = l;
    
    for(int i = l; i <= r; ++i) {
        tag[i] = g;
    }

    ::l[g] = min(::l[g], l);
    ::r[g] = max(::r[g], r);
    ::fl[g] += fl;
    ::fr[g] += fr;

    if(tmp) {
        for(int i = ::l[tmp]; i <= ::r[tmp]; ++i) {
            tag[i] = g;
        }
        ::l[g] = min(::l[g], ::l[tmp]);
        ::r[g] = max(::r[g], ::r[tmp]);
        ::fl[g] += ::fl[tmp];
        ::fr[g] += ::fr[tmp];
        ::l[tmp] = ::r[tmp] = ::fl[tmp] = ::fr[tmp] = 0;
        mer[g] = 1;
        if(fr) --::fr[g];
        if(fl) --::fl[g];
    }
    
}

void test() {
    cin>>n>>c;
    fac[0] = 1;
    for(int i = 1; i <= n; ++i) {
        cin>>a[i];
        loc[a[i]] = i;
        tag[i] = l[i] = r[i] = fl[i] = fr[i] = mer[i] = 0;
        fac[i] = fac[i - 1] * i % MOD;
    }

    int g = 0;
    ll res = 1;

    for(int val = 1; val <= n; ++val) {
        int i = loc[val];
        if(tag[i] == 0) {
            tag[i] = -1;
            add_group(i - c, i - 1, ++g, 0, 1, 1);
            add_group(i + 1, i + c, ++g, 1, 0, 1);
            continue;
        }
        if(tag[i] == -1) continue;
        add_group(l[tag[i]] - c, l[tag[i]] - 1, tag[i], 0, 1, 0);
        add_group(r[tag[i]] + 1, r[tag[i]] + c, tag[i], 1, 0, 0);
    }
    for(int i = 1; i <= g; ++i) {
        if(l[i] == 0) continue;
        if(mer[i] == 0){
            if(fl[i]) --fl[i];
            if(fr[i]) --fr[i];
        }
        // cout<<i<<' '<<l[i]<<' '<<r[i]<<' '<<fl[i]<<' '<<fr[i]<<'\n';
        for(int j = 1; j <= fl[i]; ++j) res = res * (c * j - (j - 1)) % MOD;
        for(int j = 1; j <= fr[i]; ++j) res = res * (c * j - (j - 1)) % MOD;
        res = res * fac[r[i] - l[i] + 1 - fl[i] - fr[i]] % MOD;
    }
    cout<<res<<'\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;

    cin >> T;
    while (T--) {
        test();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 17860kb

input:

5
5 3
3 4 2 1 5
5 4
4 2 1 3 5
5 2
4 5 3 1 2
5 3
4 3 2 1 5
5 2
2 3 1 5 4

output:

6
1
4
6
4

result:

ok 5 number(s): "6 1 4 6 4"

Test #2:

score: -100
Wrong Answer
time: 32ms
memory: 19836kb

input:

100000
5 4
1 3 2 5 4
5 3
5 1 4 2 3
5 2
1 4 5 3 2
5 4
5 2 4 3 1
5 4
2 5 4 1 3
5 4
1 2 3 5 4
5 4
4 3 2 5 1
5 3
1 5 4 3 2
5 3
3 2 5 4 1
5 4
4 3 1 5 2
5 4
4 3 5 2 1
5 2
3 2 1 4 5
5 3
2 4 5 1 3
5 3
2 1 4 3 5
5 3
2 1 5 4 3
5 2
2 1 3 4 5
5 4
2 3 1 4 5
5 2
1 2 4 5 3
5 3
2 4 1 5 3
5 3
2 4 3 5 1
5 3
4 1 3 5 2...

output:

24
6
4
24
1
24
24
6
18
1
24
4
6
6
6
4
1
12
1
6
6
24
18
2
18
4
6
6
18
6
4
1
6
18
1
6
24
18
6
1
12
18
6
4
2
24
12
4
24
4
4
24
6
1
1
1
1
6
1
4
1
18
1
18
4
4
6
24
6
4
6
1
12
1
4
4
6
24
18
6
2
6
1
12
6
24
1
4
4
1
1
6
1
1
24
12
18
1
4
18
1
4
24
6
4
24
6
24
1
1
6
1
18
24
1
4
1
1
2
6
1
6
4
18
1
24
6
4
4
24
...

result:

wrong answer 3rd numbers differ - expected: '6', found: '4'