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 ;
}

最新文章

  1. ASP.NET MVC 防止 CSRF 的方法
  2. Java集合类学习笔记(List集合)
  3. Spring + Spring MVC+Hibernate框架整合详细配置
  4. 小技能——markdown
  5. qt5.4 msvc2013_64安装 目标计算机不匹配问题
  6. IOS 计步器
  7. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...
  8. jQuery学习笔记----入门
  9. Allegro从.brd文件中导出器件封装
  10. libyuv颜色空间转换开源库
  11. 百度地图api的实现
  12. Junit 源码剖析(一)
  13. pushMeBaby,github链接
  14. 浏览器文档播放Shockwave Flash 插件问题
  15. [转]读取assets目录下的数据库文件
  16. vue单页应用前进刷新后退不刷新方案探讨
  17. kolla-ansible 一键安装openstack
  18. bower配置私服nexus3
  19. ubuntu,centor 安装apache bench
  20. Makefile 编译动态库文件及链接动态库

热门文章

  1. 51nod 1267二分+优化试验场
  2. Python 内置函数isinstance
  3. 剖析微软Hyper-V的最佳部署方式
  4. docker log 批量删除报错: find: `/var/lib/docker/containers/&#39;: 没有那个文件或目录
  5. SQL语句操作符优化
  6. 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra
  7. Codeforces #1063C Dwarves, Hats and Extrasensory Abilities
  8. box-sizing重置
  9. LVS Mode&amp;Method
  10. 321. Create Maximum Number 解题方法详解