QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#461523 | #7875. Queue Sorting | OvO_Zuo | WA | 39ms | 5796kb | C++14 | 1.4kb | 2024-07-02 19:55:11 | 2024-07-02 19:55:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505,mo=998244353;
int add(int x,int y){ x+=y;return x>=mo?x-mo:x;}
int C[N][N];
void init() {
int i,j;
C[0][0]=1;
for(i=1;i<N;i++) {
C[i][0]=1;
for(j=1;j<=i;j++) C[i][j]=add(C[i-1][j],C[i-1][j-1]);
}
}
int n;
int a[N];
int f[N][N];
int main() {
init();
scanf("%d",&n);
int i,j,k,p;
for(i=1;i<=n;i++) scanf("%d",&a[n-i+1]);
f[1][a[1]+1]=1;
int sum=a[1];
for(i=2;i<=n;i++) {
for(j=2;j<=sum+1;j++) {
if(!f[i-1][j]) continue;
for(k=0;k<=a[i];k++) {
if(k==a[i]) {
f[i][j+a[i]]=add(f[i][j+a[i]],f[i-1][j]);
continue;
}
for(p=1;p<j;p++)
f[i][p+k+1]=add(f[i][p+k+1],
(ll)f[i-1][j]*C[j-p-1+a[i]-k-1][a[i]-k-1]%mo);
}
}
sum+=a[i];
}
int ans=0;
for(i=1;i<=sum+1;i++) ans=add(ans,f[n][i]);
printf("%d\n",ans);
return 0;
}
/*
求包含 a[i] 个 i 的,能拆分成不超过两个不下降子序列的数列的数量
等价于不存在长为 3 的 LDS
令 i -> n-i+1, 等价于求 LIS <= 2 的数列数量
LIS = 1 时数列唯一,只需要计数 LIS = 2 的
考虑从小到大插入 i,记 f[i][j] 表示最早的 LIS = 2 的位置出现在 j
i 能且只能插在 j 之前,枚举序列的开头有几个 i,和前面有 [1,i-1] 的最靠前的 i
复杂度 O(sigma(a[i])^3)
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4728kb
input:
4 1 1 1 1
output:
14
result:
ok 1 number(s): "14"
Test #2:
score: 0
Accepted
time: 39ms
memory: 5376kb
input:
300 0 5 2 2 1 0 3 2 2 5 2 1 1 2 1 3 2 3 2 0 0 0 0 1 2 2 3 0 2 2 3 2 0 2 3 0 6 0 0 2 0 1 3 2 1 1 1 3 4 0 1 0 4 1 1 1 1 1 1 2 3 2 1 2 3 2 3 0 5 3 3 2 0 1 1 0 2 1 1 2 0 0 2 1 1 3 2 2 1 2 1 3 0 3 0 1 2 2 0 5 0 2 2 0 0 0 1 2 1 4 2 1 1 0 3 0 2 0 3 1 1 2 0 2 1 1 0 2 0 1 2 2 3 3 1 1 1 1 0 1 3 3 1 0 2 2 4 2 ...
output:
507010274
result:
ok 1 number(s): "507010274"
Test #3:
score: 0
Accepted
time: 39ms
memory: 5796kb
input:
500 1 1 0 2 1 0 2 3 2 0 0 2 0 2 1 1 0 0 1 1 1 2 1 1 1 0 1 1 2 2 1 4 0 2 1 0 2 3 1 0 1 1 0 2 1 2 2 1 0 0 3 1 4 1 1 2 1 1 0 1 3 1 2 0 0 0 2 1 2 0 0 3 2 1 1 1 1 1 2 1 0 1 0 0 0 1 0 0 2 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 0 0 1 0 0 0 2 1 1 0 1 1 0 1 1 0 0 1 0 3 1 3 0 0 2 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 2 0 0 ...
output:
7590964
result:
ok 1 number(s): "7590964"
Test #4:
score: 0
Accepted
time: 34ms
memory: 5144kb
input:
200 3 1 0 3 2 1 0 3 1 1 2 3 3 1 6 2 1 3 2 1 1 2 1 2 1 5 2 2 3 4 0 4 2 1 2 2 0 2 3 1 2 3 6 3 2 3 2 2 4 2 7 2 1 5 1 9 0 4 4 8 3 3 3 1 3 0 2 2 8 1 3 5 4 3 0 6 1 6 1 3 4 2 2 1 1 4 4 4 1 0 4 3 4 3 3 0 3 2 0 0 3 4 0 3 1 3 2 4 3 2 0 3 2 2 3 2 2 2 1 2 2 1 0 2 0 3 1 3 5 1 3 3 6 5 3 2 2 2 3 6 2 0 5 2 2 2 2 1 ...
output:
507844569
result:
ok 1 number(s): "507844569"
Test #5:
score: 0
Accepted
time: 9ms
memory: 5056kb
input:
100 4 8 2 5 4 4 3 0 2 7 2 3 4 4 1 2 3 4 4 4 3 3 3 3 3 2 4 1 3 5 5 1 4 6 1 1 1 3 2 3 2 1 0 1 4 4 2 4 2 5 3 5 1 6 2 3 3 1 4 4 4 1 4 4 3 4 2 0 2 3 6 1 3 3 5 4 1 1 2 3 0 3 2 2 1 3 3 2 5 6 3 2 3 3 5 4 2 3 4 4
output:
989550242
result:
ok 1 number(s): "989550242"
Test #6:
score: 0
Accepted
time: 1ms
memory: 4944kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: -100
Wrong Answer
time: 1ms
memory: 4748kb
input:
500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'