题目链接:

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. jquery中美元符号($)命名冲突

    http://blog.csdn.net/shanshan209/article/details/6341727 在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替 ...

  2. win10任务管理器开机老是自己打开

    win10任务管理器开机老是自己打开 学习了:https://zhidao.baidu.com/question/332868722086816045.html 还没有注意过这个东西:系统失败-> ...

  3. Exchange 2013 Database Move to New Partition

    建议不要删除默认数据库,可以通过修改默认数据库名称.路径等实现您的需求. 客戶:The HK Anti-Cancer Society. 要求:遷移數據庫(01)到新分區,實際是遷移成為數據庫(05)  ...

  4. C++简单介绍

    一.怎样用C++的源文件产生一个可运行程序 一个C++程序由一个或者多个编译单元组成.每一个编译单元都是一个独立的源码文件.一般是一个带.cpp的文件,编译器每次编一个文件编译单元,生成一个以.obj ...

  5. C语言-回溯例3

    排列问题 1.实现排列A(n,m)对指定的正整数m,n(约定1<m<=n),具体实现排列A(n,m).2. 回溯算法设计设置一维数组a,a(i)(i=1,2,…,m)在1—n中取值.首先从 ...

  6. Linux基础(2)- 用户、群组和权限

    一.用户.群组和权限 1)  新建用户natasha,uid为1100,gid为555,备注信息为“master” 2)  修改natasha用户的家目录为/Natasha 3)  查看用户信息配置文 ...

  7. Android多线程下载大文件解析

    1.多线程介绍 用过迅雷的同学都知道.迅雷有个功能叫做多线程.另一个叫离线下载,我们这里重点介绍一下多线程下载.多线程,顾名思义就是非常多歌线程同一时候在执行,为什么要提出多线程这个概念呢?由于有时候 ...

  8. javascript 高级编程系列 - 创建对象

    1. 工厂模式 function createPerson(name, age) { var obj = {}; obj.name = name; obj.age = age; obj.getName ...

  9. vim调试

    首先,想调试一个程序的话,输入以下命令: guest-djjtew@ubuntu:~$ python3 -m pdb 1.py 这时候就停止了,等待着你的输入,然后输入"l"的话, ...

  10. Redis 过期键的设置、获取和删除过期时间

    Redis 过期键的设置.获取和删除过期时间 转自http://blog.51cto.com/littledevil/1813956 设置过期 默认情况下键是没有生存时间的,也就是永不过期,除非清空内 ...