题目链接:

Ordered Subsequence

Time Limit: 4000/2000 MS (Java/Others)  

  Memory Limit: 32768/32768 K (Java/Others)

Problem Description
 
A numeric sequence of ai is ordered if a1<a2<……<aN. Let the subsequence of the given numeric sequence (a1, a2,……, aN) be any sequence (ai1, ai2,……, aiK), where 1<=i1<i2 <……<iK<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others.

Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.

 
Input
 
Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
 
Output
 
For each case, output answer % 123456789.
 
Sample Input
3 2
1 1 2
7 3
1 7 3 5 9 4 8
 
Sample Output
2
12
 
题意:
 
求长为n的数组中的长度为m的单调递增子序列的个数;
 
思路:
 
跟又一次的CF一样,只不过这题还要离散化;
dp[i][j]表示以第j个结尾长为i的子序列的个数;
 
 
AC代码:
 
  1. /*4991 655MS 9664K 1701 B G++ 2014300227*/
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. const int N=1e4+;
  5. typedef long long ll;
  6. const ll mod=;
  7. int n,m;
  8. ll sum[N],dp[][N];
  9. int lowbit(int x)
  10. {
  11. return x&(-x);
  12. }
  13. void update(int x,ll num)
  14. {
  15. while(x<=n)
  16. {
  17. sum[x]+=num;
  18. sum[x]%=mod;
  19. x+=lowbit(x);
  20. }
  21. }
  22. ll query(int x)
  23. {
  24. ll s=;
  25. while(x>)
  26. {
  27. s+=sum[x];
  28. s%=mod;
  29. x-=lowbit(x);
  30. }
  31. return s;
  32. }
  33. struct node
  34. {
  35. int num,pos,c,d;
  36. };
  37. node po[N];
  38. int cmp1(node x,node y)
  39. {
  40. if(x.num==y.num)return x.pos<y.pos;
  41. return x.num<y.num;
  42. }
  43. int cmp2(node x,node y)
  44. {
  45. return x.pos<y.pos;
  46. }
  47. int main()
  48. {
  49. while(scanf("%d%d",&n,&m)!=EOF)
  50. {
  51. for(int i=;i<=n;i++)scanf("%d",&po[i].num),po[i].pos=i;
  52. sort(po+,po+n+,cmp1);
  53. po[].num=-;
  54. for(int i=;i<=n;i++)
  55. {
  56. if(po[i].num==po[i-].num)
  57. {
  58. po[i].c=po[i-].c;
  59. }
  60. else po[i].c=i;//po[i].c表示第一个跟po[i].num相同的数的位置;
  61. po[i].d=i;//表示po[i]插入时的位置;
  62. }
  63. sort(po+,po+n+,cmp2);
  64. for(int i=;i<=n;i++)
  65. {
  66. dp[][i]=;
  67. update(po[i].d,);
  68. }
  69. for(int i=;i<=m;i++)
  70. {
  71. memset(sum,,sizeof(sum));
  72. for(int j=;j<=n;j++)
  73. {
  74. if(po[j].c>)
  75. dp[i][j]=query(po[j].c-);//转移方程;
  76. else dp[i][j]=;
  77. update(po[j].d,dp[i-][j]);//把dp[i-1][j]更新上去;
  78. }
  79. }
  80. ll ans=;
  81. for(int i=;i<=n;i++)
  82. {
  83. ans+=dp[m][i];
  84. ans%=mod;
  85. }
  86. printf("%lld\n",ans);
  87. }
  88. return ;
  89. }
 

hdu-4991 Ordered Subsequence(dp+树状数组)的更多相关文章

  1. HDU4991 Ordered Subsequence (树状数组优化DP)

    dp[i][j]表示以a[i]结尾的长度为j的上升子序列个数. 方程:dp[i][j]=sum(dp[k][j-1]),a[k]<a[i],1<=k<i. 求解目标:sum(dp[k ...

  2. HDU 2836 Traversal 简单DP + 树状数组

    题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个. dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( he ...

  3. HDU 5489 Removed Interval DP 树状数组

    题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...

  4. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  5. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  6. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  7. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  8. 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...

  9. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

随机推荐

  1. Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息。

    Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息.”的问题解决 2015-01-22 09:49:02 标签:Windows Server2003 PerfDi ...

  2. react/redux组件库、模板、学习教程

    开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...

  3. JavaScript中的Math方法演示

    <html> <head> <script type="text/javascript"> var num = 12.4; alert(Math ...

  4. Python基础语法03-控制流

    Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和 ...

  5. 【matlab】:matlab中不断的出现计算过程怎么办

    这个问题是会常常性出的.就是matlab中不断的出现计算. 关于这个问题,我们须要考虑的是自己是不是写错了,通常会出现以下两种可能的错误 1,关于计算的函数没有写分号 :这样的是致命问题,假设函数不写 ...

  6. Ural 2018The Debut Album(DP)

    题目地址:Ural 2018 简单DP.用滚动数组. 代码例如以下: #include <iostream> #include <cstdio> #include <st ...

  7. Item 51:写new和delete时请遵循惯例

    Item 51: Adhere to convention when writing new and delete. Item 50介绍了怎样自己定义new和delete但没有解释你必须遵循的惯例. ...

  8. 安装mongoDB遇见的一个路径问题

    如果安装路径不存在,则不会解压EXE软件! 安装monogoDB后,它不会自动添加执行路径! 意思就是安装路径是D盘下面的mongoDB文件夹,假如不存在这个文件夹,则不会安装成功 你需要添加路径: ...

  9. C 标准库 - <signal.h>

    C 标准库 - <signal.h> 简介 signal.h 头文件定义了一个变量类型 sig_atomic_t.两个函数调用和一些宏来处理程序执行期间报告的不同信号. 库变量 下面是头文 ...

  10. 微信小程序制作商 业务流程