C - The Battle of Chibi

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值