Ordered Subsequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 464    Accepted Submission(s): 216

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 的上升子序列.
题解:dp[i][j]代表以第 i 个元素结尾,长度为 j 的子序列 的个数。那么 dp[i][j] = sum(dp[k][j-1]) (1<=k<j).
但是这样的话我们直接枚举是 O(n*n*m)这样的时间复杂度是接受不了的。所以求和的那一层用树状数组优化成 O(n*log(n)*m)
/**
状态转移方程:
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)的更多相关文章

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

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

  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. 【树状数组+dp】HDU 5542 The Battle of Chibi

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

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

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

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

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

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

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

随机推荐

  1. GoF23种设计模式之创建型模式之建造者模式

    一.概述 将一个复杂对象的构建与其表示分离开来,使得同样的构建过程可以创建不同的表示. 二.适用性 1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式的时候. 2.当构造过程必须允许 ...

  2. 在windows和Linux下安装nodejs

    在windows下安装nodejs 1.首先下载nodejs安装包,  https://nodejs.org/en/download/ 点击下载相应的版本 然后将文件夹解压到安装目录(任意,不做规定) ...

  3. A1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  4. CSU-2007 Football Training Camp

    Football Training Camp 在一次足球联合训练中一共有n支队伍相互进行了若干场比赛. 对于每场比赛,赢了的队伍得3分,输了的队伍不得分,如果为平局则两支队伍各得1分. Input 输 ...

  5. Group Policy Object Editor

    Group Policy Object Editor   The Group Policy Object Editor is a tool that hosts MMC extension snap- ...

  6. IOS开发学习笔记029-反选、全选、删除按钮的实现

    还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...

  7. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  8. 拼多多2018校招编程题汇总 Python实现

    题目原址 列表补全 在商城的某个位置有一个商品列表,该列表是由L1.L2两个子列表拼接而成.当用户浏览并翻页时,需要从列表L1.L2中获取商品进行展示.展示规则如下: 用户可以进行多次翻页,用offs ...

  9. Linux 基础命令、文档树 和 bash

    最近发现了一个总结得更好的:bash cheatsheet 本文只是我对 linux 基础学习的一个总结,可能仅适用于复习用.算是我的 Linux 备忘录. 最基础 tab 补全 * 通配符 ctrl ...

  10. [python][django 1.10中文文档]

    https://docs.djangoproject.com/en/1.10/  官方文档,点我下载 推荐一个翻译django 1.8.2的网址: 推荐一个翻译django 1.10的博客:(着重推荐 ...