hdu 4991(树状数组+DP)
Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 464 Accepted Submission(s): 216
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
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
1 1 2
7 3
1 7 3 5 9 4 8
12
/**
状态转移方程:
dp[i][j] = sum(dp[k][j-1]) (1<=k<i&&a[k]<a[i])
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL mod = ;
const int N = ;
const int M = ;
int cnt,n,m;
LL dp[N][M]; ///dp[i][j]代表第 i 个元素结尾,长度为 j 的递增子序列个数.
LL c[N],b[N],a[N]; LL lowbit(int i){
return i&(-i);
}
void update(int idx,int x,LL v){
for(int i=idx;i<=cnt;i+=lowbit(i)){
dp[i][x]=(dp[i][x]+v)%mod;
}
}
LL getsum(int idx,int x){
LL sum = ;
for(int i=idx;i>;i-=lowbit(i)){
sum=(sum+dp[i][x])%mod;
}
return sum;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
b[i] = a[i];
}
cnt = ;
for(int i=;i<=n;i++){ ///离散化 a 数组对应树状数组的 1 - cnt
if(b[i]!=b[i-]){
b[++cnt] = b[i];
}
}
sort(b+,b+cnt+);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
int idx = lower_bound(b+,b++cnt,a[i])-b;
for(int j=;j<=m;j++){
LL v;
if(j == ) v = ;
else v = getsum(idx-,j-);
update(idx,j,v);
}
}
LL ans = getsum(cnt,m);
printf("%lld\n",ans);
}
return ;
}
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
const int mod=;
int n,m;
int a[];
int b[],cnt; inline void Add(int &a,int b){
a=(a+b)%mod;
}
inline int lowbit(int x){
return x&(-x);
}
int sum[][];
inline void add(int id,int x,int v){
while(x<=cnt){
Add(sum[id][x],v);
x+=lowbit(x);
}
}
inline int query(int id,int x){
int ans=;
while(x){
Add(ans,sum[id][x]);
x-=lowbit(x);
}
return ans;
} int main(){
while(~scanf("%d%d",&n,&m)){
cnt=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[++cnt]=a[i];
}
sort(b+,b+cnt+);
cnt=(int)(unique(b+,b++cnt)-(b+));
for(int i=;i<=n;i++)a[i]=(int)(lower_bound(b+,b++cnt,a[i])-b); memset(sum,,sizeof sum); int ans=;
for(int i=;i<=n;i++){
for(int j=;j<m;j++){
if(a[i]>){
int sum=query(j,a[i]-);
add(j+,a[i],sum);
}
}
add(,a[i],);
}
ans=query(m,cnt);
printf("%d\n",ans);
}
return ;
}
hdu 4991(树状数组+DP)的更多相关文章
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- 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 ...
- 【树状数组+dp】HDU 5542 The Battle of Chibi
http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结 ...
- HDU 6348 序列计数 (树状数组 + DP)
序列计数 Time Limit: 4500/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)
题意: 二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动, 该N个点只有从它的左下方格点可达,此时可获得收益.求该过程最大收益. 分析:我们很容易就可以想到用 ...
随机推荐
- Vue2.0--14.小白入门教程--实例化多个vue对象,可初始化操作几种方法
课程地址: https://study.163.com/course/courseMain.htm?courseId=1004711010 <!DOCTYPE html> <html ...
- jQuery的select2下拉框的搜索功能(使用select2插件,方便简单)
第一步: 引入我们用使用的插件 jquery: select2: css: js: 第二步: 创建一个html页面,body内容: <div> <select class=" ...
- day 63 Django基础九之中间件
Django基础九之中间件 本节目录 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证 六 xxx 七 xxx 八 xxx 一 前戏 我们在前面的课程中已经学 ...
- 精通Spring Boot---使用@ControllerAdvice处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- Python3爬取起点中文网阅读量信息,解决文字反爬~~~附源代码
起点中文网,在“数字”上设置了文字反爬,使用了自定义的文字文件ttf通过浏览器的“检查”显示的是“□”,但是可以在网页源代码中找到映射后的数字正则爬的是网页源代码,xpath是默认utf-8解析网页数 ...
- Kubernetes master服务定制编译docker镜像
前言 之前部署了Kubernetes 1.13.0,发现master服务的启动方式与1.10.4版本有所区别,kube-apiserver.kube-controller-manager和kube-s ...
- Windows 2008 WinRM和WinRS能帮你做什么?
介绍 WinRM及WinRS更新包含在Windows Vista, Windows Server 2003 R2, Windows Server 2008 (及 Server 2008 Cor ...
- Singleton模式类 【微软面试100题 第七十二题】
题目要求: 实现C++单例模式,即只能生成一个实例的类. 题目分析: 1.一般情况:用构造函数私有化和静态函数实现. 2.如果考虑内存泄露:用智能指针+一般情况方法. 3.如果考虑线程安全:加锁. 代 ...
- 分享一个文件查找、替换制定的字符或数字之CS程序、附带源码
首先就上操作流程图: 图--登陆界面.登陆密码:alidoing.com 图--界面说明(一看就懂) 图--文件查找到再替换 图--文件替换成功 图--替换后的文件 代码开始: 登陆的代码就非常简单. ...
- leetcode 【 Insertion Sort List 】 python 实现
题目: Sort a linked list using insertion sort. 代码:oj测试通过 Runtime: 860 ms # Definition for singly-linke ...