hdu-Find the nondecreasing subsequences(树状数组)

本文介绍了一种结合离散化技术与动态规划思想的算法实现方案,通过具体代码展示了如何利用离散化来优化DP算法的过程。适用于解决特定类型的最优化问题。

详细解释点击:here~~

离散化  ++++ dp思想

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int mod = 1000000007;
int a[100010];
int n;
struct node
{
    int x,y;
}s[100010];

bool cmp(node p,node q)
{
    if(p.x == q.x) return p.y < q.y;
    return p.x < q.x;
}
int lowbit(int x)
{
    return x&(-x);
}
void add(int pos,int num)
{
    while(pos <= n)
    {
        a[pos] = (a[pos] + num) % mod;
        pos += lowbit(pos);
    }
}
int getsum(int pos)
{
    int res = 0;
    while(pos > 0)
    {
        res = (res + a[pos]) % mod;
        pos -= lowbit(pos);
    }
    return res;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(s,0,sizeof(s));
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&s[i].x);
            s[i].y = i;
        }
        sort(s+1,s+n+1,cmp);
        int temp = 0;
        for(int i = 1;i <= n;i++)
        {
            temp = getsum(s[i].y) + 1;
            temp %= mod;
            add(s[i].y,temp);
        }
        printf("%d\n",getsum(n));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值