QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#168701#5047. Permutationabsinthe#WA 30ms17768kbC++202.3kb2023-09-08 20:02:122023-09-08 20:02:13

Judging History

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

  • [2023-09-08 20:02:13]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:17768kb
  • [2023-09-08 20:02: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 || r > n)) return;
    int tmp = 0;
    l = max(l, 1);
    r = min(r, n);
    for(int i = l; i <= r; ++i) {
        if(tag[i] == -1) return;
        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;
    }
    
}

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;
        }
        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]) --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: 17768kb

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: 30ms
memory: 15804kb

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

result:

wrong answer 32nd numbers differ - expected: '1', found: '2'