UESTC 1217 The Battle of Chibi

dp+树状数组优化。

dp[i][j]表示以a[i]结尾,最长上升序列长度为j的方案数。dp[i][j]=sum{dp[k][j-1]} 其中k<i&&a[k]<a[i]。 离散化后,可以用1000个树状数组维护。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi = acos(-1.0), eps = 1e-8;
void File()
{
    freopen("D:\\in.txt", "r", stdin);
    freopen("D:\\out.txt", "w", stdout);
}
inline int read()
{
    char c = getchar();  while (!isdigit(c)) c = getchar();
    int x = 0;
    while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
    return x;
}

int mod=1000000007;
const int maxn=1010;
int a[maxn],b[maxn],c[maxn][maxn],dp[maxn][maxn],ans;
int T,n,m;

int lowbit(int x) { return x&(-x); }
int sum(int r,int p) { int res=0; for(int i=p;i>0;i=i-lowbit(i)) res=(res+c[r][i])%mod; return res; }
void update(int r,int p,int v) { for(int i=p;i<=1001;i=i+lowbit(i)) c[r][i]=(c[r][i]+v)%mod; }

int main()
{
    scanf("%d",&T); int cas=1;
    while(T--)
    {
        scanf("%d%d",&n,&m); memset(c,0,sizeof c); ans=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i-1]=a[i];
        sort(b, b + n); int sz = unique(b, b + n) - b;
        for(int i=1;i<=n;i++) a[i]=lower_bound(b, b + sz, a[i])-b+2;
        for(int i=1;i<=n;i++)
        {
            dp[i][1]=1; update(1,a[i],dp[i][1]);
            for(int j=2;j<=m;j++) { dp[i][j]=sum(j-1,a[i]-1); update(j,a[i],dp[i][j]); }
        }
        for(int i=1;i<=n;i++) ans=(ans+dp[i][m])%mod;
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}

 

posted @ 2016-08-01 19:05  Fighting_Heart  阅读(171)  评论(0编辑  收藏  举报