。。很久以前看过题面然后没有仔细想,再做的时候忘了序列是随机的了。。然后怎么搞都是
O(n4)
的。
我们可以将状态设为f(x,i,l,r),表示在i次操作后,[l,r]<x,l-1和r+1大于等于x的方案数。(不妨认为
a[0]=a[n+1]=∞
)这样的话假如说有跨越区间端点的操作,那么就一定会使操作中的数≥x。然后令g(x,j)表示让位置j最终<x的方案数,那么
g(x,j)=∑jl=1∑nr=jf(x,q,l,r)
,
∑xx∗g(x+1,j)−g(x,j)
就是答案了。
然而这样的话状态数似乎是
O(n4)
的。。但是对于
a[i]=x
而言,可能成为x的位置只在它左右第一个比它大的位置之间,而且数据随机。数据随机就有笛卡尔树高是
O(lgn)
的,那么一个简单但不严谨的想法就是可以把笛卡尔树看成一个满二叉树,那么状态数就是
O(∑log2ni=12i−1(n2i)2)=O(∑log2ni=1n22i)=O(n2)
。一个稍微严谨一点的想法是基于随机n个数,那么第n个数最大的概率是
1n
,这样的话就有
O(∑ni=1∑ij=2(∏j−1k=2k−1k)1jj2)=O(n2)
.所以总的状态数其实只有
O(n2q)
。
再考虑转移,
注意到后面两个相当于是对一段区间加同一个数,类似差分一下什么的就很好搞了。
代码:
#include<cstdio>
#include<iostream>
using namespace std;
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long LL;
const int N=400+5,Q=400+5;
int n,q;
const int Mod=1e9+7;
inline LL power(LL prod,int x)
{
LL ans=1;
for(;x;x>>=1)
{
if(x&1)ans=ans*prod%Mod;
prod=prod*prod%Mod;
}
return ans;
}
LL f[2][N][N];
int a[N],hash[N];
LL g[N][N];
int stack[N];
int chain[N][N];
inline void cal(int L,int R,int x)
{
if(++L>--R)return;
//printf("---cal([%d,%d],%d)---\n",L,R,hash[x]);
int now=0,post=1;
for(int l=L;l<=R;++l)memset(f[0][l]+l,0,sizeof(LL)*(R-l+1));
f[0][L][R]=1;
for(int i=1;i<=q;++i)
{
swap(now,post);
for(int l=L;l<=R;++l)memset(f[now][l]+l,0,sizeof(LL)*(R-l+1));
for(int r=L;r<=R;++r)
{
LL s=0;
for(int l=L;l<=r;++l)
{
f[now][l][r]+=s;
s+=(l-1)*f[post][l][r];
}
}
for(int l=L;l<=R;++l)
{
LL s=0;
for(int r=R;r>=l;--r)
{
f[now][l][r]+=s;
s+=(n-r)*f[post][l][r];
}
}
for(int l=L;l<=R;++l)
for(int r=l;r<=R;++r)
(f[now][l][r]+=(((l-1)*l>>1)+((r-l+1)*(r-l+2)>>1)+((n-r)*(n-r+1)>>1))*f[post][l][r])%=Mod;
/*for(int l=L;l<=R;++l)
for(int r=l;r<=R;++r)
if(f[now][l][r])
printf("f(%d,[%d,%d])=%I64d\n",i,l,r,f[now][l][r]);*/
}
for(int l=L;l<=R;++l)
for(int r=l;r<=R;++r)
for(int j=l;j<=r;++j)
g[x][j]+=f[now][l][r];
for(int j=L;j<=R;++j)g[x][j]%=Mod;
for(int j=L;j<=R;++j)chain[j][++chain[j][0]]=x;
}
int main()
{
freopen("uoj196.in","r",stdin);
freopen("uoj196.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)
{
scanf("%d",a+i);
hash[i]=a[i];
}
sort(hash+1,hash+n+1);
int htot=unique(hash+1,hash+n+1)-hash;
for(int i=n;i;--i)a[i]=lower_bound(hash+1,hash+htot,a[i])-hash;
a[0]=htot,a[n+1]=htot,hash[htot]=-1;
int top=1;
for(int i=1;i<=n;++i)
{
for(;top&&a[stack[top-1]]<a[i];--top)cal(stack[top-1],i,a[stack[top-1]]);
if(top)
{
cal(stack[top-1],i,a[i]);
if(a[stack[top-1]]==a[i])--top;
}
stack[top++]=i;
}
while(--top)cal(stack[top],n+1,a[stack[top]]);
g[htot][1]=power(n*(n+1)>>1,q);
for(int i=n;i>1;--i)g[htot][i]=g[htot][1];
for(int i=1;i<=n;++i)
{
chain[i][++chain[i][0]]=a[i];
sort(chain[i]+1,chain[i]+chain[i][0]+1);
chain[i][0]=unique(chain[i]+1,chain[i]+chain[i][0]+1)-chain[i];
chain[i][chain[i][0]]=htot;
//printf("----%d----\n",i);
//for(int j=1;j<=chain[i][0];++j)printf("g[%d]=%I64d\n",hash[chain[i][j]],g[chain[i][j]][i]);
LL ans=0;
for(int j=1;j<chain[i][0];++j)(ans+=hash[chain[i][j]]*(g[chain[i][j+1]][i]-g[chain[i][j]][i]))%=Mod;
(ans+=Mod)%=Mod;
if(i!=1)putchar(' ');
printf("%lld",ans);
//puts("");
}
puts("");
}
总结:
①一定要仔细读题,注意变量的上下界,是否有特殊约定。
②如果确定不会爆long long的话,可以最后再模,这样可以减小常数。
③#define rep这种东西感觉挺科学的,以后要学着写的。