题意:

给你一个序列a[],求它的不降子序列的个数

分析:

dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]);答案就是sum(dp[i])

但发现一个问题n很大O(n^2)肯定超时,想起前面做的两道题,用线段树优化,它是用维护的是和,可以用BIT优化,但又发现a[i]的值很大没法存,就又想到了离散化,恩这个题就解决了。

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <cstdio>
  8. #include <vector>
  9. #include <string>
  10. #include <cctype>
  11. #include <complex>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. typedef pair<int,int> PII;
  20. typedef long long ll;
  21. #define lson l,m,rt<<1
  22. #define pi acos(-1.0)
  23. #define rson m+1,r,rt<<11
  24. #define All 1,N,1
  25. #define N 100010
  26. #define read freopen("in.txt", "r", stdin)
  27. const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
  28. const int INF= 0x7ffffff;
  29. const int mod = ;
  30. ll a[N],bit[N],tmp[N],dp[N];
  31. int n;
  32. void add(int x,ll d){
  33. while(x<=n){
  34. bit[x]=(bit[x]+d)%mod;
  35. x+=(x&(-x));
  36. }
  37. }
  38. ll sum(int x){
  39. ll num=;
  40. while(x>){
  41. num=(num+bit[x])%mod;
  42. x-=(x&(-x));
  43. }
  44. return num;
  45. }
  46. int main()
  47. {
  48. while(~scanf("%d",&n)){
  49. for(int i=;i<=n;++i){
  50. scanf("%I64d",&a[i]);
  51. tmp[i]=a[i];
  52. }
  53. sort(tmp+,tmp+n+);
  54. int len=unique(tmp+,tmp+n+)-tmp-;
  55. /*for(int i=1;i<=len;++i)
  56. cout<<tmp[i]<<endl;*/
  57. memset(dp,,sizeof(dp));
  58. memset(bit,,sizeof(bit));
  59. ll total=;
  60. for(int i=;i<=n;++i){
  61. int pos=lower_bound(tmp+,tmp+len+,a[i])-tmp;
  62. dp[i]=;
  63. dp[i]=(dp[i]+sum(pos))%mod;
  64. add(pos,dp[i]);
  65. total=(total+dp[i])%mod;
  66. }
  67. printf("%I64d\n",total);
  68. }
  69. return ;
  70. }

HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)的更多相关文章

  1. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

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

  2. HDU 2227 Find the nondecreasing subsequences(DP)

    Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...

  3. HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...

  4. HDU 2227 Find the nondecreasing subsequences (线段树)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. HDU 2227 Find the nondecreasing subsequences (数状数组)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 2227 Find the nondecreasing subsequences

    题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...

  7. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  8. HDU 3506 (环形石子合并)区间dp+四边形优化

    Monkey Party Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Tot ...

  9. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. hdu 2227(树状数组+dp)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

随机推荐

  1. Winform基础 -- 菜单

    快速创建默认菜单 使用控件 MenuStrip : 点击菜单的右上方小三角:选择 [插入标准项] 即可显现出标准的菜单格式: 如果想添加更多的菜单项,可以在   [请在此处键入] 处输入菜单项的名称 ...

  2. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  3. PHP输入流php://input [转]

    对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...

  4. shutdown -s -t

    import java.io.*; import java.awt.*; public class HackDemo{ public static void main(String args[])th ...

  5. 应用程序加载外部字体文件(使用AddFontResource API函数指定字体)

    /* MSDN: Any application that adds or removes fonts from the system font table should notify other w ...

  6. Quartz所使用的表的说明

    Quartz将Job保存在数据库中所需表的说明 QRTZ_CALENDARS 以 Blob 类型存储 Quartz 的 Calendar 信息 QRTZ_CRON_TRIGGERS 存储 Cron T ...

  7. Android 类似时间轴的实现

    想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...

  8. Android LayoutInflater.from(context).inflate

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  9. idea 找不到 没有 tomcat server

    follow me 1. 2. 3. 4.

  10. c 语言练习__求到N的阶乘的和。

    #include <stdio.h> /* 题目如下 * S = 1 + 2! + 3! + ... + N! */ int main(int argc, char *argv[]) { ...