《[ARC127E] Priority Queue》解题报告
秒了,感觉没有道理的,感觉比前面的题简单,过的人少可能是因为位置放的太后了,不会做
首先问我们最后序列的可能情况,我们一般考虑结果序列可行的充要条件。
容易发现倘若结果序列中删除了
也就是说倘若删了一个数
所以我们只需要找到所有删除都最靠前的情况即可,然后计算方案数只需要考虑把删除往后放就可以了。
考虑从小到大填
考虑从后往前删数,记
时间复杂度
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int N=5010,MAXN=1e4+10,P=998244353;
int n,m;
int a[MAXN],l[MAXN],tot,cnt;
int f[N],g[N],s[N];
bool sf[MAXN];
LL fad(LL x,LL y) {
return (x+y>=P?x+y-P:x+y);
}
signed main () {
scanf("%d%d",&n,&m);
for(int i=1;i<=n+m;++i) {
scanf("%d",&a[i]);
}
for(int i=1;i<=n+m;++i) {
if(a[i]==1) l[++tot]=++cnt;
else sf[l[tot--]]=1;
}
f[n+1]=1;
for(int i=n;i>=1;--i) {
if(!sf[i]) continue;
for(int j=n+1;j>=i;--j) {
s[j]=fad(s[j+1],f[j]);
}
for(int j=i;j<=n;++j) {
g[j]=s[j+1];
}
for(int j=i;j<=n+1;++j) {
f[j]=g[j];
}
}
LL ans=0;
for(int i=1;i<=n+1;++i) {
ans=fad(ans,f[i]);
}
printf("%lld\n",ans);
return 0;
}