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, the 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 of this sequence 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.
给出一个长度为N的数字串,找出一个严格上升的数字序列来.

Input

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

Output

Output 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

这道题对于现在的我来说已经算是水题了,但是当年我还是看了半个小时才看懂。

f[i]表示到i最长的上升序列

主要的思路就是枚举每个点,然后再与后面的点比较,加入后面的点比他大,长度就+1,即f[j]>f[i],f[j]=f[i]+1;(当然前提是f[i]+1要比原本的f[j]要大)

代码:

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int s[],n,a[],ans;
  5. int main()
  6. {
  7.     scanf("%d",&n);
  8.     for(int i=;i<=n;i++)
  9.     {
  10.         scanf("%d",&a[i]);
  11.         s[i]=;
  12.     }
  13.     for(int i=;i<=n;i++)
  14.         for(int j=i+;j<=n;j++)
  15.             if(a[i]<a[j]&&s[i]+>=s[j])s[j]=s[i]+,ans=max(ans,s[j]);
  16.     printf("%d",ans);
  17. }

这是O(N^2)的算法,当数据大一些的时候就不行了。

众所皆知,大多数O(N^2)的算法可以用二分优化到O(N log(N))。

没错,就是我们可以用一个数组来存,但是这个数组存的并不是答案,只是当前形成的上升序列,每进来一个数,都用二分查找第一个比他小的数,然后取而代之,如果没有比他小的数,就放到最后面,数组最后的元素个数就是答案。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int f[];
  4. int t,m,n;
  5. int main()
  6. {
  7. int m;
  8. cin>>m;
  9. for(int i=;i<=m;i++)
  10. {
  11. int ans=;
  12. cin>>n;
  13. for(int i=;i<=n;i++)
  14. {
  15.  
  16. cin>>t;
  17. if(i==) f[++ans]=t;
  18. else
  19. {
  20. if(t>f[ans]) f[++ans]=t;
  21. else
  22. {
  23. int x=lower_bound(f+,f+ans,t)-f;
  24. f[x]=t;
  25. }
  26. }
  27. }
  28. cout<<ans<<endl;
  29. }
  30. return ;
  31. }

其实还有树状数组的做法,这里不给出了

最长上升序列(Lis)的更多相关文章

  1. (LIS)最长上升序列(DP+二分优化)

    求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...

  2. XHXJ's LIS HDU - 4352 最长递增序列&数位dp

    代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...

  3. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  4. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  5. 算法面试题 之 最长递增子序列 LIS

    找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E ...

  6. 最长上升子序列LIS(51nod1134)

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递 ...

  7. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

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

  8. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...

  9. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...

  10. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

随机推荐

  1. 有待总结的KMP算法 sdut oj 2463 学密码学一定得学程序

    学密码学一定得学程序 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 曾经,ZYJ同学非常喜欢密码 学.有一天,他发现了一个很长很 ...

  2. complex brain network

    Organization, development and function of complex brain networks The Brain as a Complex System: Usin ...

  3. centos6.5 mysql 5.6修改root密码,以及创建用户并授权

    mkdir -p mysql_home/{data,temp,undologs,logs} chown -R mysql:mysql /dbfiles/mysql_home mysql_install ...

  4. codeforces C. New Year Ratings Change 解题报告

    题目链接:http://codeforces.com/problemset/problem/379/C 题目意思:有n个users,每个user都有自己想升的rating.要解决的问题是给予每个人不同 ...

  5. hdu-5726 GCD(rmq)

    题目链接: GCD Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Prob ...

  6. redis-cluster的实例动态调整内存

    当redis.conf中的最大内存配置为10G的时候,恰好程序已经写满了,但是物理主机是有内存的, 此时可以通过config set xxxx xxxx 来设置实例的内存大小,而不需要重启实例. 获取 ...

  7. BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset

    BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i ...

  8. 从exgcd到exCRT

    从最基础的开始. 1.gcd 这个不用说了吧--\(gcd(a,b) = gcd(b,a\%b)\),这个很显然. 2.exgcd 这玩意可以用来求形如\(ax+by = gcd(a,b)\)的不定方 ...

  9. 中缀表达式std

    #include<cstdio>#include<cstdlib>#include<string>#include<cstring>using name ...

  10. Tutte矩阵求一般图最大匹配

    [集训队2017论文集] 一张无向图的Tutte矩阵为 其中xi,j为一个random的值. Tutte矩阵的秩(一定为偶数)/2 就是这张图的最大匹配. 原理大概就是: 一个图有完美匹配,则det( ...