HDU4991 Ordered Subsequence (dp+树状数组+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4991

题目的意思是求长度为 m 的上升序列有多少个;

设DP[I][J]表示 以第i个元素结尾的 长度为j的序列的个数

dp[i][j]=sum(dp[k][j-1])  (i>k &&a[i]>a[k])

数字有1万个,先离散化一下,把所有数字对应到1到n之间,然后用数组数组进行求和就好
代码如下:
#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

const int N = 10010;

const int mod = 123456789;

typedef long long LL;

LL c[N],a[N],b[N];
int n,m;
LL dp[N][104];

int lowbit(int x)
{
    return x & (-x);
}

void update(int pos,LL val)
{
    while(pos <= n)
    {
        c[pos] += val;
        pos += lowbit(pos);
    }
}

LL query(int pos)
{
    LL res = 0;
    while(pos > 0)
    {
        res = (res+c[pos])%mod;
        pos -= lowbit(pos);
    }
    return res;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            b[i] = a[i];
        }
        sort(b+1,b+n+1);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            dp[i][1] = 1;
        for(int j=2;j<=m;j++)
        {
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;i++)
            {
                int ind = lower_bound(b+1,b+n+1,a[i])-b;
                dp[i][j] = query(ind-1);
                update(ind,dp[i][j-1]);
            }
        }
        LL ans = 0;
        for(int i=1;i<=n;i++)
            ans = (ans + dp[i][m])%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值