【BZOJ4361】isn
题目
做法
\(dp_{i,j}\)表示以\(i\)结尾\(j\)长度,树状数组\(tree_{i,j}\)表长度为\(i\),以\(<=j\)结尾的个数,显然\(dp_{i,j}=\sum\limits_{k=1}^{pos[i]}tree[j-1][k]\)
从而\(O(n^2logn)\)得到每个长度不下降子序列个数
\(ans=\sum\limits_{i=1}^n(g[i]\times (n-i)!-g[i+1]\times (n-i-1)!\times (i+1))\)
怎么理解这句话呢?\(g[i]\)表长度\(i\)的个数,我们确定了长度,自然能得到这个长度的方案数,然而有些是不合法的,因为之前已经得到不下降了不能再删
那就减去\(i+1\)的个数到\(i\)的转移
My complete code
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
const LL maxn=3000;
const LL p=1e9+7;
inline LL Read(){
LL x(0),f=1; char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
LL n,cnt,ans;
LL tree[maxn][maxn],a[maxn],b[maxn],g[maxn],dp[maxn][maxn],pos[maxn],jc[maxn];
inline LL Lowbit(LL x){
return x&(-x);
}
inline LL Query(LL len,LL x){
LL ret(0);
for(;x;x-=Lowbit(x))
ret=(ret+tree[len][x]);
return ret;
}
inline void Add(LL len,LL x,LL val){
for(;x<=n;x+=Lowbit(x))
tree[len][x]=(tree[len][x]+val)%p;
}
int main(){
n=Read();
for(LL i=1;i<=n;++i)
a[i]=b[i]=Read();
sort(b+1,b+1+n);
cnt=unique(b+1,b+1+n)-b-1;
Add(0,1,1);
for(LL i=1;i<=n;++i){
pos[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
for(LL j=i;j>=1;--j){
dp[i][j]=Query(j-1,pos[i])%p;
Add(j,pos[i],dp[i][j]);
}
}
for(LL i=1;i<=n;++i)
for(LL j=1;j<=n;++j)
g[i]=(g[i]+dp[j][i])%p;
jc[1]=1;
for(LL i=2;i<=n;++i)
jc[i]=jc[i-1]*i%p;
for(LL i=1;i<=n;++i)
ans=(ans+g[i]*jc[n-i]%p-g[i+1]*jc[n-i-1]%p*(i+1)%p+p)%p;
printf("%lld\n",ans);
return 0;
}