题目链接:

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. Hierarchical data in postgres

    https://coderwall.com/p/whf3-a/hierarchical-data-in-postgres --------------------------------------- ...

  2. 视图交互--表视图(UITableView)的cell交互析略

    在表视图UITableView的cell上经常有一些交互,根据项目开发中的情况,需要对此进行一些规范.总结出了几种交互方法,这些方法在其他视图的交互上同样可以适用.用一个简单的例子来举例说明一下,其他 ...

  3. CMake - boost - 可运行程序 - 静态库

    CMake - boost 最后更新日期:2014-04-25by kagula 阅读前提:<CMake入门(二)>.Linux的基本操作 环境: Windows 8.1 64bit英文版 ...

  4. Cursor类用法:

      Cursor类用法:   http://www.2cto.com/kf/201109/103163.html   Ctrl+Shift+G 查找类.方法和属性的引用.这是一个非常实用的快捷键,例如 ...

  5. MySQL 下优化SQL语句的一些经验

    http://java-guru.iteye.com/blog/143377

  6. Python+Selenium ----unittest单元测试框架

    unittest是一个单元测试框架,是Python编程的单元测试框架.有时候,也做叫做“PyUnit”,是Junit的Python语言版本.这里了解下,Junit是Java语言的单元测试框架,Java ...

  7. leetCode(40):Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. Cadence SPB 16. 6 安装步骤

    1.首先下载Cadence Allegro SPB orCAD16. 6 安装包,单击我,下载之后运行其中的setup.exe,然后先安装第一项License Manager

  9. HDU-4930 Fighting the Landlords 多校训练赛斗地主

    仅仅须要推断一个回合就能够了,枚举推断能够一次出全然部牌或者大过对面的牌的可能,注意的是4张同样的牌带两张牌的话是能够被炸弹炸的. #include <iostream> #include ...

  10. MonoTouch.Dialog简介

    新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...