Longest Ordered Subsequence

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence ( ai1ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

  1. 7
  2. 1 7 3 5 9 4 8

Sample Output

  1. 4
  2.  
  3. LIS最长上升子序列问题。(可加二分优化O(nlogn))f[i]记录以当前值作为最后一个数时的最大长度。每枚举一个数都找前面比他小且f[i]最大的状态+1f[i]=max(f[j])+1
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int f[],a[];
  5.  
  6. int max(int x,int y)
  7. {
  8. return x>y?x:y;
  9. }
  10.  
  11. int main()
  12. {
  13. int n,i,j;
  14. scanf("%d",&n);
  15. memset(a,,sizeof(a));
  16. memset(f,,sizeof(f));
  17. for(i=;i<=n;i++){
  18. scanf("%d",&a[i]);
  19. }
  20. f[]=;
  21. for(i=;i<=n;i++){
  22. for(j=;j<i;j++){
  23. if(a[j]<a[i]){
  24. f[i]=max(f[i],f[j]);
  25. }
  26. }
  27. f[i]++;
  28. }
  29. int ans=;
  30. for(i=;i<=n;i++){
  31. ans=max(ans,f[i]);
  32. }
  33. printf("%d\n",ans);
  34. return ;
  35. }
  1.  

最少拦截系统

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统. 
 
Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔) 
Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统. 
 
Sample Input

  1. 8 389 207 155 300 299 170 158 65

Sample Output

  1. 2
  1. 贪心解决最少序列个数问题。数组记录每个序列的最小值,出现比他大的数存入新下标,数组长度即为个数。
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int a[];
  5.  
  6. int main()
  7. {
  8. int n,x,c,i,j;
  9. while(~scanf("%d",&n)){
  10. c=;
  11. memset(a,,sizeof(a));
  12. a[]=;
  13. for(i=;i<=n;i++){
  14. scanf("%d",&x);
  15. for(j=;j<=c;j++){
  16. if(a[j]>=x){
  17. a[j]=x;
  18. break;
  19. }
  20. if(j==c){
  21. a[++c]=x;
  22. break;
  23. }
  24. }
  25. }
  26. printf("%d\n",c);
  27. }
  28. return ;
  29. }
  1.  

POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)的更多相关文章

  1. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  2. POJ 2533 Longest Ordered Subsequence(裸LIS)

    传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  3. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  4. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  5. POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)

    传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...

  6. POJ 2533 Longest Ordered Subsequence 最长递增序列

      Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

  7. poj 2533 Longest Ordered Subsequence(LIS)

    Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of ...

  8. POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38980   Acc ...

  9. Poj 2533 Longest Ordered Subsequence(LIS)

    一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

随机推荐

  1. iOS 流布局 UICollectionView使用(简单使用)

    简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似.简单来说,UICollectionView是比UI ...

  2. Flask:工厂函数和蓝本

    我们用pycharm去新建Flask项目的时候,会默认生成开发文件.如下,其中包括static,templates,flask1_prj.py文件 在最初开始的时候,我们的app等声明都是在flask ...

  3. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  4. luoguP3066 [USACO12DEC]逃跑的BarnRunning

    luoguP3066 [USACO12DEC]逃跑的BarnRunning 题目大意 给定一棵n个节点的树和参数L,查询每个节点子树中到达该节点距离<=L的数量(包括该节点) 偏模板的主席树 P ...

  5. P1856 [USACO5.5]矩形周长Picture

    P1856 [USACO5.5]矩形周长Picture $len$            $sum$              $num$             $flag\_l$ $flage\_ ...

  6. IC卡、ID卡、M1卡、射频卡的区别是什么【转】

    本文转载自:https://www.cnblogs.com/najifu-jason/p/4122741.html IC卡.ID卡.M1卡.射频卡都是我们常见的一种智能卡,但是很多的顾客还是不清楚IC ...

  7. Django+ajax+jsonp实现借口调用文本处理

    首页 提交 <script src="/static/jquery-2.1.4.min.js"></script> <script type=&quo ...

  8. hadoop —— teragen & terasort

    这两个类所在目录: hadoop-examples-0.20.2-cdh3u6.jar 中: 代码: TeraGen.java: /** * Licensed to the Apache Softwa ...

  9. web tools for sublime

    Your code editor is your main development tool; you use it to write and save lines of code. Write be ...

  10. c++的最小整数和最大整数

    #include<iostream> #include<cmath> using namespace std; int main() { //int -2147483648~2 ...