Ordered Subsequence

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

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
Source
BestCoder Round #8
官方题解
1003 Ordered Subsequence
首先数字有1万个,先离散化一下,把所有数字对应到1到n之间。这样对结果不影响。
dp[i][j]代表以第i个数字结尾上升子序列长度为j的种数。
dp[i][j]=sum{dp[k][j-1]}  for each a[k]<a[i]&&k<i
直接写循环会超时。需要优化。
可以用平衡树进行优化,上述的循环过程可以看成是一个区间求和过程。用线段树或者树状数组可以解决。
这样最终的复杂度是n*m*log(n)

这里我用100个数组数组搞了搞,注意中间的取模
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#define maxn 10010
#define LL long long
#define INF 999999
#define mod 123456789LL
using namespace std;struct node
{int id;LL val;bool operator <(const node&s)const{return val < s.val ;}
}qe[maxn];LL xt[101][maxn] ;
int n ,a[maxn] ;
LL dp[maxn][101] ;
void insert(int id,int x,int add)
{while( x <= n ){xt[id][x]+= add;if(xt[id][x] >= mod) xt[id][x] -= mod;x += (x&-x) ;}
}
LL sum(int id,int x)
{LL ans=0;while(x >0){ans += xt[id][x] ;if(ans>=mod) ans -= mod;x -= (x&-x) ;}return ans;
}
int main()
{int m,i,j ;while( scanf("%d%d",&n,&m) != EOF){for( i = 1 ; i <= n ;i++){scanf("%I64d",&qe[i].val) ;qe[i].id= i;}sort(qe+1,qe+1+n) ;j = 1 ;a[qe[1].id] = j ;for( i = 2 ; i <= n ;i++){if(qe[i].val==qe[i-1].val) a[qe[i].id]=j ;else a[qe[i].id] = ++j;}memset(xt,0,sizeof(xt)) ;memset(dp,0,sizeof(dp)) ;LL ans=0;for(i = 1 ; i <= n ;i++){dp[i][1] = 1 ;for( j = 2 ; j <= m && j <= i ;j++){dp[i][j] = sum(j-1,a[i]-1) ;}ans = (ans+dp[i][m])%mod;for( j = 1 ; j <= m && j <= i;j++){insert(j,a[i],dp[i][j]) ;}}cout << ans << endl;}return 0 ;
}

  

 

转载于:https://www.cnblogs.com/20120125llcai/p/3961621.html

hdu 4991 Ordered Subsequence相关推荐

  1. HDU 4990 Ordered Subsequence --数据结构优化DP

    题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...

  2. hdu 4991(树状数组优化dp)

    Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. 【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)

    题干: Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Accepted: 18453 Description ...

  4. Longest Ordered Subsequence 最长上升子序列+DP

    A numeric sequence of ai is ordered if a1 < a2 < - < aN. Let the subsequence of the given n ...

  5. 10027Longest Ordered Subsequence Extention

    A numeric sequence of ai is ordered if a1 < a2 < - < aN. Let the subsequence of the given n ...

  6. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  7. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  8. scau实验题 8596 Longest Ordered Subsequence

    其实解释POJ 2533 原题 最长上升子序列,用O(N*N)算法和o(n*logn)算法分别实现,学校OJ的数据比较弱,就算写成最长不下降子序列也可以通过,但是在POJ上是WA的 而题意本身应该是严 ...

  9. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence

    1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...

最新文章

  1. Centos7安装防火墙firewall
  2. centos7安装mongodb3.6
  3. linux非交互式脚本,Linux expect非交互式执行脚本
  4. Java 理论与实践: 流行的原子
  5. C# 观察文件的更改
  6. oracle导入视图报错,exp/imp 报错处理(EXP-00003 / IMP-00019 / IMP-00058)
  7. ShuffleNetv2的学习笔记
  8. 电磁冷坩埚行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  9. INFO: task java:27465 blocked for more than 120 seconds不一定是cache太大的问题
  10. 由于本机的限制,该操作已被取消。请与系统管理员联系
  11. Service 中的 onStart 和 onStartCommand
  12. SDL Trados Studio 2019 免费Google机器翻译插件安装和使用
  13. SpringMVC复习
  14. 付永刚计算机信息安全技术课后答案
  15. BitLocker加密怎么解除?
  16. ipv6连接数据库oracle,oracle连接数据库方式大全
  17. emqttd 2.2安装和测试使用
  18. C++ pair 和make_pair
  19. Uaexpert操作手册
  20. [转]彻底卸载SQL Server2014数据库(也适用于SqlServer2012)

热门文章

  1. Android省电妙招
  2. 让iis记录nginx反向代理真实ip
  3. 应用虚拟化之规划篇二 项目流程规划
  4. window启动过程讲解--PPT截图[张银奎]
  5. TypeError: 'str' object is not callable
  6. shell脚本报错问题: -bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory
  7. 都是成年人了,别再相信OA价格越低越好这种话了
  8. 流利说公布上市后首份财报:第三季净收入1.8亿
  9. 部分高级正则特性 使用
  10. Android线程和线程Handler基础一览