Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army. But all generals and soldiers of Cao Cao were loyal, it's impossible to convince any of them to betray Cao Cao.
So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.
Yu Zhou discussed with Gai Huang and worked out NN information to be leaked, in happening order. Each of the information was estimated to has aiai value in Cao Cao's opinion.
Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact MM information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the NN information and just select MM of them. Find out how many ways Gai Huang could do this.
题意:求长度为n的上升子序列个数。
只想到了o(n^3)的暴力,没想到用树状数组优化
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
struct no
{
int v,i;
bool operator <(const no &a)const
{
return v<a.v;
}
};
no a[1010],b[1010];
int dp[1010][1010],c[1010];
int f[1010];
const long long mod=1e9+7;
long long s0(int i)
{
long long ans=0;
for(; i>0; ans=(ans+c[i])%mod,i-=(i&(-i)));
return ans;
}
void s1(int i,long long v,int m)
{
for(; i<=m; c[i]=(c[i]+v)%mod,i+=(i&(-i)));
}
int main()
{
int m,n,T;
cin>>T;
for(int cas=1; cas<=T; cas++)
{
memset(dp,0,sizeof(dp));
memset(f,0,sizeof(f));
scanf("%d%d",&m,&n);
for(int i=1; i<=m; i++)
{
scanf("%d",&a[i].v);
a[i].i=i;
}
sort(a+1,a+1+m);
int top=1;
//for(int i=1;i<=m;i++)if(f[a[i]]==0)f[a[i]]=top++;
for(int i=1; i<=m; i++)
if(i==1||a[i].v!=a[i-1].v)f[a[i].i]=++top;
else f[a[i].i]=top;
long long ans=0;
for(int i=1; i<=m; i++)dp[1][i]=1;
for(int i=2; i<=n; i++)
{
memset(c,0,sizeof(c));
for(int j=1; j<=m; j++)
{
dp[i][j]=s0(f[j]-1);
s1(f[j],dp[i-1][j],top);
}
}
for(int i=1; i<=m; i++)ans=(ans+dp[n][i])%mod;
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}