hdu 2227(dp+树状数组)

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

Find the nondecreasing subsequences

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1466    Accepted Submission(s): 521


Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
 

Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
 

Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
 

Sample Input
  
  
3 1 2 3
 

Sample Output
  
  
7
 

Author
8600
思路:dp[i] 表示以第i个数为结尾的不递减子序列的个数;

            那么状态转移方程式:dp[i]=sum( dp[k] ) +1;  其中a[i]>a[k]&&k<i; 这个应该好想

            最朴素的方法是O(n*n),但是在这题中n的数据太大,会超时,所以再想到树状数组动态维护一段区间的和,所以先把n个数离散化一下,然后再树状数组求和;

PS:另外再说两点 (1)这题和hdu4991类似;都是dp+离散化+树状数组

                                    (2)说实话,这题题意不明确,我都想了半天,举个例子

                                        INPUT: 3

                                                         1 1 2

                                       OUTPUT: 7

                                        按AC程序:不递减子序列有7个分别为: (1),(1,1),(1),(2),(1,2),(1,2),(1,1,2),按题目的意思是对重复的组合也得算进去,这地方有点坑;

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
const int mod=1000000007;
using namespace std;

struct node
{
    int val, id;
}a[100005];

bool cmp(node a, node b)
{
    return a.val < b.val;
}

int b[100005], c[100005], s[100005],dp[100005],n;

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

void update(int i, int x)
{
    while(i <= n)
    {
        s[i] += x;
        if(s[i] >= mod)
            s[i] %= 1000000007;
        i += lowbit(i);
    }
}

int getsum(int i)
{
    int sum = 0;
    while(i > 0)
    {
        sum += s[i];
        if(sum >= 1000000007)
            sum %= 1000000007;
        i -= lowbit(i);
    }
    return sum;
}

int main()
{

    while(scanf("%d", &n) != EOF)
    {
        memset(b, 0, sizeof(b));
        memset(s, 0, sizeof(s));
        memset(c,0,sizeof(c));

        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i].val);
            a[i].id = i;
        }
        sort(a+1, a+n+1, cmp);
        b[a[1].id] = 1;

        for(int i = 2; i <= n; i++)
        {
            if(a[i].val != a[i-1].val)
                b[a[i].id] = i;
            else b[a[i].id] = b[a[i-1].id];
        }

        for(int i=1;i<=100005;i++)dp[i]=1;

        for(int i=1;i<=n;i++)
        {
          dp[i]=dp[i]+getsum(b[i]);
          update(b[i],dp[i]);
        }

        int ans=0;
        for(int i=1;i<=n;i++)
         ans=(ans+dp[i])%mod;

        printf("%d\n",ans);
        /*res = 0;                      //两种写法
        for(i = 1; i <= n; i++)
        {
            c[i] = sum(b[i]);
            update(b[i], c[i]+1);
        }
        printf("%d\n", sum(n));
       */
    }


    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值