QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#168804#5047. PermutationabsintheWA 27ms17776kbC++202.8kb2023-09-08 22:28:122023-09-08 22:28:13

Judging History

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

  • [2023-09-08 22:28:13]
  • 评测
  • 测评结果:WA
  • 用时:27ms
  • 内存:17776kb
  • [2023-09-08 22:28:12]
  • 提交

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 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;
        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] = 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(fl[i] == 0 || fr[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();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 17712kb

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: 0
Accepted
time: 27ms
memory: 17776kb

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
6
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
6
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
6
4
24
...

result:

ok 100000 numbers

Test #3:

score: -100
Wrong Answer
time: 3ms
memory: 15732kb

input:

10000
10 2
7 4 2 9 1 3 10 5 8 6
10 7
10 6 1 5 2 4 7 9 8 3
10 4
10 7 6 2 8 1 5 4 3 9
10 4
4 9 6 2 10 7 8 5 1 3
10 5
9 8 7 4 5 3 2 10 6 1
10 3
5 7 8 1 9 4 6 10 3 2
10 4
6 2 10 8 4 7 9 5 3 1
10 5
10 1 3 7 5 9 4 2 8 6
10 7
10 3 4 5 6 1 9 2 8 7
10 4
1 8 9 7 5 6 3 10 4 2
10 3
6 7 3 1 9 4 10 8 5 2
10 3
5 1...

output:

432
5040
2304
24
201600
720
5040
120
1
11520
720
120
1
216
120
120
2160
141120
144
1
480
1
108
24
1
432
25200
1
30240
35280
35280
720
1800
576
1296
120
35280
20160
432
432
8
201600
96
432
141120
201600
2304
720
1152
576
1
120
201600
576
360
241920
40320
24
1
1
1
35280
1
1
1
3600
720
108
720
1
1296
1...

result:

wrong answer 10th numbers differ - expected: '20160', found: '11520'