题目链接:

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代码:
 
/*4991    655MS    9664K    1701 B    G++    2014300227*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=;
int n,m;
ll sum[N],dp[][N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,ll num)
{
while(x<=n)
{
sum[x]+=num;
sum[x]%=mod;
x+=lowbit(x);
}
}
ll query(int x)
{
ll s=;
while(x>)
{
s+=sum[x];
s%=mod;
x-=lowbit(x);
}
return s;
}
struct node
{
int num,pos,c,d;
};
node po[N];
int cmp1(node x,node y)
{
if(x.num==y.num)return x.pos<y.pos;
return x.num<y.num;
}
int cmp2(node x,node y)
{
return x.pos<y.pos;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)scanf("%d",&po[i].num),po[i].pos=i;
sort(po+,po+n+,cmp1);
po[].num=-;
for(int i=;i<=n;i++)
{
if(po[i].num==po[i-].num)
{
po[i].c=po[i-].c;
}
else po[i].c=i;//po[i].c表示第一个跟po[i].num相同的数的位置;
po[i].d=i;//表示po[i]插入时的位置;
}
sort(po+,po+n+,cmp2);
for(int i=;i<=n;i++)
{
dp[][i]=;
update(po[i].d,);
}
for(int i=;i<=m;i++)
{
memset(sum,,sizeof(sum));
for(int j=;j<=n;j++)
{
if(po[j].c>)
dp[i][j]=query(po[j].c-);//转移方程;
else dp[i][j]=;
update(po[j].d,dp[i-][j]);//把dp[i-1][j]更新上去;
}
}
ll ans=;
for(int i=;i<=n;i++)
{
ans+=dp[m][i];
ans%=mod;
}
printf("%lld\n",ans);
}
return ;
}
 

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. tensorflow global_variables_initializer()

    老版本为 init = tf.initialize_all_variables() 新版本为 init = tf.global_variables_initializer()

  2. Python下opencv使用笔记(七)(图像梯度与边缘检測)

    梯度简单来说就是求导,在图像上表现出来的就是提取图像的边缘(无论是横向的.纵向的.斜方向的等等),所须要的无非也是一个核模板.模板的不同结果也不同.所以能够看到,全部的这些个算子函数,归结究竟都能够用 ...

  3. LNMPA遇到504 Gateway time-out错误的解决方法

    Nginx的特点是处理静态很给力,Apache的特点是处理动态很稳定,两者结合起来便是LNMPA,nginx处理前端,apache处理后端,这样处理静态会很快,处理动态会很稳定. 当我以为安装完成以后 ...

  4. oracle [union.minus.intersect]

    union 两张表的相同字段的数据[记录类型和列数要一致],合并,并且去重 can replace with "in" (但是如果是两个不同的表而且没什么关联的话必须要union了 ...

  5. 测试 MD

    上面是一张图片 总店?

  6. ok6410[001] Ubuntu 16.04[64bit]嵌入式交叉编译环境arm-linux-gcc搭建过程图解

    开发PC:Ubuntu16.04.1 开发板:OK6410[飞凌公司出品] 目标:通过GPIO点亮LED ----------------------------------------------- ...

  7. vs2010音频文件压缩 调用lame_enc.dll将WAV格式转换成MP3

    /* //My_lame.h */ #pragma once#include "stdafx.h"#include <windows.h>#include <st ...

  8. 二进制安装Mysql 5.6(免编译)

    安装系统基础软包 yum install -y make bc perl gcc openssl openssl-devel ncurses ncurses-devel 安装方式:二进制免编译安装 查 ...

  9. poj1125--Floyd

    题解: 有N个股票经济人能够互相传递消息.他们之间存在一些单向的通信路径.如今有一个消息要由某个人開始传递给其它全部人.问应该由哪一个人来传递,才干在最短时间内让全部人都接收到消息. 显然,用Floy ...

  10. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...