Find the nondecreasing subsequences

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

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
 
题意:找到一个字符串里面所有的不下降子序列的数量之和。
题解:设dp[i] 以a[i]结尾的非递减子串的个数,可以知道 dp[i] = sum(dp[j])+1 (1<=j<i,a[j]<a[i]),这里a[j]<a[i]我们可以直接利用求解逆序数的原理得到,利用树状数组维护整个递推式。
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stack>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8. typedef long long LL;
  9. const int N = ;
  10. const int mod = ;
  11. int n;
  12. int a[N],b[N],c[N];
  13. int lowbit(int x){
  14. return x&(-x);
  15. }
  16. void update(int idx,int v){
  17. for(int i=idx;i<=n;i+=lowbit(i)){
  18. c[i]=(c[i]+v)%mod;
  19. }
  20. }
  21. int getsum(int idx){
  22. int sum = ;
  23. for(int i=idx;i>=;i-=lowbit(i)){
  24. sum = (sum+c[i])%mod;
  25. }
  26. return sum;
  27. }
  28. int main()
  29. {
  30. while(scanf("%d",&n)!=EOF){
  31. memset(c,,sizeof(c));
  32. for(int i=;i<=n;i++){
  33. scanf("%d",&a[i]);
  34. b[i] = a[i];
  35. }
  36. sort(b+,b++n);
  37. int ans = ;
  38. for(int i=;i<=n;i++){
  39. int idx = lower_bound(b+,b++n,a[i])-b;
  40. ans=getsum(idx);
  41. update(idx,ans+);
  42. }
  43. printf("%d\n",getsum(n));
  44. }
  45. return ;
  46. }

hdu 2227(树状数组+dp)的更多相关文章

  1. hdu 4991(树状数组+DP)

    Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  5. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  6. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. 【树状数组+dp】HDU 5542 The Battle of Chibi

    http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结 ...

  8. HDU 6348 序列计数 (树状数组 + DP)

    序列计数 Time Limit: 4500/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  9. HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)

    题意: 二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动, 该N个点只有从它的左下方格点可达,此时可获得收益.求该过程最大收益. 分析:我们很容易就可以想到用 ...

随机推荐

  1. 怎么使用formBuilder以拖拽方式编写页面

    1.以admin用户登录系统,打开formbuilder http://localhost:8081/framework/main/formbuilder 2.从右方组件中,用鼠标拖拽页面所需的组件到 ...

  2. 数据添加到solr索引库后前台如何搜索

    主要结构: 查询 Dao: package com.taotao.search.dao.impl; import java.util.ArrayList; import java.util.List; ...

  3. SQLite 学习笔记

    SQLite 学习笔记. 一.SQLite 安装    访问http://www.sqlite.org/download.html下载对应的文件.    1.在 Windows 上安装 SQLite. ...

  4. ACM2112迪克斯特算法

    HDU Today Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区50 ...

  5. CSS知识之 background-position 用法详细介绍

    一.语法 background-position : length || length background-position : position || position 二.取值 length   ...

  6. C++ 指针常见用法小结

    1. 概论 2.指针基础 3. 指针进阶 4. 一维数组的定义与初始化 5. 指针和数组 6. 指针运算 7. 多维数组和指针 8. 指针形参 9. 数组形参 10. 返回指针和数组 11. 结语   ...

  7. 11.nginx upload module + python django 后台 实现视频上传与切片

    1.需求:支持视频上传并切片,支持通过m3u8文件播放 2.视频切片的上一节已经谈过,这一节主要是视频上传的处理 第一步:upload-module模块安装 -----------首先下载upload ...

  8. 数据结构:Bitset

    这个东西看起来很棒棒的样子呀 bitset存储二进制数位 bitset就像一个bool类型的数组一样 bitset中的每个元素都能单独被访问 整数类型和布尔数组都能转化成bitset 有关Bitset ...

  9. HDU 5961 传递 BFS

    题意:中文题,就是判断一个竞赛图拆成两个图,判断是否都传递 思路:分别BFS判深度即可,用这种方法注意要进行读入优化. /** @Date : 2016-11-18-20.00 * @Author : ...

  10. Redis 键值数据类型及基本操作

    到目前为止,Redis 支持的键值数据类型如下: 字符串(String) 哈希(Map) 列表(list) 集合(sets) 有序集合(sorted sets)   1. String 字符串类型 s ...