QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#398888#5047. Permutation2745518585WA 42ms10196kbC++202.2kb2024-04-25 19:29:572024-04-25 19:29:58

Judging History

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

  • [2024-04-25 19:29:58]
  • 评测
  • 测评结果:WA
  • 用时:42ms
  • 内存:10196kb
  • [2024-04-25 19:29:57]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
const int N=1000001;
const ll P=998244353;
int n,k,a[N],b[N];
ll jc[N];
struct str
{
    int l,r,ls,rs;
}f[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            b[a[i]]=i;
        }
        jc[0]=1;
        for(int i=1;i<=n;++i) jc[i]=jc[i-1]*i%P;
        set<int> Set;
        for(int i=1;i<=n;++i) f[i].l=f[i].r=f[i].ls=f[i].rs=0;
        if(b[1]-1>=k)
        {
            Set.insert(0);
            f[0].l=0,f[0].r=b[1],f[0].ls=0,f[0].rs=k;
        }
        if(n-b[1]>=k)
        {
            Set.insert(b[1]);
            f[b[1]].l=b[1],f[b[1]].r=n+1,f[b[1]].ls=k,f[b[1]].rs=0;
        }
        ll s=1;
        for(int i=1;i<=n;++i)
        {
            int t=b[i];
            auto p=upper_bound(Set.begin(),Set.end(),t);
            if(p==Set.begin()) continue;
            int x=*prev(p);
            if(f[x].r<=t) continue;
            if(t<=f[x].l+f[x].ls)
            {
                s=s*f[x].ls%P;
                f[x].ls+=k;
            }
            else if(t>=f[x].r-f[x].rs)
            {
                s=s*f[x].rs%P;
                f[x].rs+=k;
            }
            else
            {
                {
                    f[t].l=t,f[t].r=f[x].r,f[t].ls=0,f[t].rs=f[x].rs;
                    if(t+k<f[t].r) f[t].ls+=k;
                    Set.insert(t);
                    if(f[t].ls+f[t].rs>=f[t].r-f[t].l-1)
                    {
                        s=s*jc[min(f[t].r,n)-max(f[t].l,1)+1-(f[t].ls+f[t].rs)/k]%P;
                        Set.erase(t);
                    }
                }
                {
                    f[x].r=t,f[x].rs=0;
                    if(t-k>f[x].l) f[x].rs+=k;
                }
            }
            if(f[x].ls+f[x].rs>=f[x].r-f[x].l-1)
            {
                s=s*jc[min(f[x].r,n)-max(f[x].l,1)+1-(f[x].ls+f[x].rs)/k]%P;
                Set.erase(x);
            }
        }
        printf("%lld\n",s);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 42ms
memory: 10196kb

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

result:

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