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
解题思路:这道题和hdu 5542完全一样,长度为m的递增子序列的个数,树状数组+dp。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
using namespace std;const int maxn = 10005;
const int mod = 123456789;
int n,m;
int tree[105][maxn],a[maxn];
set<int> Set;
map<int,int> Map;int lowbit(int x)
{return x & -x;
}void update(int i,int x,int c)
{while(x <= n){tree[i][x] = (tree[i][x] + c) % mod;x += lowbit(x);}
}int getsum(int i,int x)
{int sum = 0;while(x > 0){sum = (sum + tree[i][x]) % mod;x -= lowbit(x);}return sum;
}int main()
{while(scanf("%d%d",&n,&m)!=EOF){Map.clear();Set.clear();memset(tree,0,sizeof(tree));for(int i = 1; i <= n; i++){scanf("%d",&a[i]);Set.insert(a[i]);}int cnt = 0;for(set<int>::iterator it = Set.begin(); it != Set.end(); it++)Map[*it] = ++cnt;for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){int tmp = 0;if(j == 1) tmp = 1;else tmp = (tmp + getsum(j-1,Map[a[i]]-1)) % mod;update(j,Map[a[i]],tmp);}printf("%d\n",getsum(m,cnt));}return 0;
}

hdu 4991(树状数组优化dp)相关推荐

  1. E. Pencils and Boxes (树状数组优化dp)

    传送门 1.题意 给出n个数,要求给这些数分组,每组不少于k个. 每组的数之间的差不能大于d.是否能够分组? 2.dp分析 先排序. f[i]f[i]f[i]表示[1~i]能够分组成功. 答案就是f[ ...

  2. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  3. [SCOI2014]方伯伯的玉米田 //二维树状数组优化DP

    题目: 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美.这排玉米一共有N株,它们的高度参差不齐.方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感的玉米拔除掉,使得剩 ...

  4. hdu 5542(树状数组优化dp)

    题意: 求n个数中长度为m的上升子序列的个数 解题思路:dp[i][j]表示第i个数长度为j的上升序列的个数.dp[i][j] = sum{dp[k][j-1] | a[k] < a[i]},这 ...

  5. HDU 6447 YJJ's Salesman(树状数组优化DP + 离散化)

    HDU 6447 YJJ's Salesman 题目 给一个二维数组,从(0,0)走到(1e9, 1e9).每次只能走右,下,右下,三个方向.其中只有通过右下走到特定给出的点(村庄)时才会获得分值.问 ...

  6. 【HDU - 6447】YJJ's Salesman(降维dp,树状数组优化dp)

    题干: YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is ...

  7. bzoj 1264: [AHOI2006]基因匹配Match (树状数组优化dp)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1264 思路: n大小为20000*5,而一般的dp求最长公共子序列复杂度是 n*n的,所以我 ...

  8. acwing 297. 赤壁之战 树状数组优化DP 寒假集训

    题目链接 想要求长度为M的子序列,我们可以拿DP方程来计算,并且这个DP也是比较好看出来的DP[i][j]代表着i后j位置中的所有长度为j的子序列,递推方程为 for(int i=1;i<=n; ...

  9. CF1621G Weighted Increasing Subsequences(离散化+树状数组优化dp+栈维护后缀最大值+计数)

    problem luogu-link solution 显然单独考虑每个 iii 的贡献,即被多少个合法上升子序列包含. 令 x=max⁡{j∣j>i∧aj>ai}x=\max\{j\ | ...

最新文章

  1. 赛灵思Zynq-7000 可扩展处理平台(EPP)介绍
  2. Asp.net实现在线截图(大图截取为小图)
  3. android Map集合的遍历
  4. IOS学习之多线程(2)--创建线程
  5. SLAM - 01 - 分类
  6. 中职计算机高考总分是多少,高考理科总分是多少分
  7. Linux文本复制到记事本文本文件乱码,解决“在windows里的记事本里编辑的汉字文本文件,上传到linux服务器上出现乱码“问题...
  8. linux 系统一键安装 lnmp
  9. 【小项目】学生信息登记系统
  10. VS发布 错误 未能将文件 复制到
  11. django学习笔记01
  12. c语言怎么求一个数的所有因数,【代码】求一个数的因数和、求优化、顺便也供新人参考算法...
  13. 西门子1200伺服步进FB块程序 含触摸屏程序,SCL写的FB块,中文注释详细
  14. 局域网下的两个 QQ 好友进行在线文件传输抓包
  15. Axure 9 简介【原型工具】
  16. 计算机二级实践网上教程答案,全国计算机等级二级教程课后习题+答案
  17. 学习笔记15-L298N
  18. 计算机中的文件怎么加密文件,想把电脑里的文件加密怎么弄?
  19. 安装VMware15虚拟机+Ubuntu19.10
  20. AE开发 遇到未能加载文件或程序集的问题 FileNotFoundException

热门文章

  1. 神策 2021 数据驱动大会嘉宾阵容首曝 + 精彩观点前瞻
  2. javascript +new Date()
  3. QQ 相册后台存储架构重构与跨 IDC 容灾实践
  4. 关闭sql执行功能及找回08CMS系统管理员密码
  5. Java构造和解析Json数据之org.json
  6. C语言库函数大全及应用实例十四
  7. [征求意见]团队发展、技术交流主题、团队机构
  8. r shiny app learning tutorial a sliderinput
  9. C++模板特化的一个BUG?
  10. 空场景在安卓上的渲染消耗问题