首先得明白一个概念:子序列不一定是连续的,可以是断开的。

有两种写法:

一、动态规划写法

复杂度:O(n^2)

代码:

 #include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define INF 0x3f3f3f3f using namespace std;
typedef long long ll;
const int maxn = ;
int a[maxn],dp[maxn]; int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n && n)
{
for(int i = ; i<n; i++)
cin>>a[i];
int mmax = -;
for(int i = ; i<n; i++)
{
dp[i] = ;
for(int j = ; j<i; j++)//遍历之前的每一个元素pre
{
if(a[j]<a[i] && (dp[j]+>dp[i]))//如果元素pre < 当前元素cur,而且pre的长度+1要比当前的长度多就更新当前的长度
{
dp[i] = dp[j]+;
}
}
mmax = max(mmax,dp[i]);//维护最大的长度
}
printf("%d\n",mmax);
}
return ;
}

二、low_bound写法

复杂度:O(nlogn)

这种写法就是将动规写法中的第二层的遍历改为了二分查找。所以复杂度变为O(nlogn)

牛客讲解视频

该算法中开了一个辅助数组h来表示该长度下最后的元素的最小值。例如 1、2、0、5 、-1

为什么要修改h数组里边的数为小的值呢,因为修改后h数组能变长的潜力就增大了,所以要修改。

代码:

 #include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define INF 0x3f3f3f3f using namespace std;
typedef long long ll;
const int maxn = ;
int a[maxn],dp[maxn]; int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
memset(a,,sizeof(a));
for(int i = ; i<n; i++)
dp[i] = INF;
for(int i = ; i<n; i++)
cin>>a[i];
int mmax = -;
for(int i = ; i<n; i++)
{
int k = lower_bound(dp, dp+n, a[i])-dp;
dp[k] = a[i];
mmax = max(mmax, k+);
}
printf("%d\n",mmax);
}
return ;
}

LIS(两种方法求最长上升子序列)的更多相关文章

  1. 求逆元的两种方法+求逆元的O(n)递推算法

    到国庆假期都是复习阶段..所以把一些东西整理重温一下. gcd(a,p)=1,ax≡1(%p),则x为a的逆元.注意前提:gcd(a,p)=1; 方法一:拓展欧几里得 gcd(a,p)=1,ax≡1( ...

  2. Python3求笛卡尔积的两种方法

    [本文出自天外归云的博客园] 电影异次元杀阵三部曲中密室线索反复出现笛卡尔积的运用.百度百科: 笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为 ...

  3. Redis中持久化的两种方法详解

    Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里;另一种方法教只追加文件(append-only f ...

  4. Linux安装MySQL的两种方法

    转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1.       运行平台:CentOS 6.3 x86_64,基本等同于RH ...

  5. C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法。

    原文:C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法. 一般我们在开发Windows Phone App时,需要取得萤幕的大小来自定义最佳化控制项的大小,但是开如何取得萤 ...

  6. 原生Js 两种方法实现页面关键字高亮显示

    原生Js 两种方法实现页面关键字高亮显示 上网看了看别人写的,不是兼容问题就是代码繁琐,自己琢磨了一下用两种方法都可以实现,各有利弊. 方法一 依靠正则表达式修改 1.获取obj的html2.统一替换 ...

  7. 用js写出光棒效应的两种方法与jquery的两中方法

    <script src="js/jQuery1.11.1.js" type="text/javascript"></script> &l ...

  8. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  9. 代码操作Word时,目录自动更新的两种方法

    最近的项目中有一个功能点为:根据分析数据库并生成报告.不过不是大数据.数据挖掘之类,报告的内容.组织方式都是事先固定下来的.实现的方式为,在普通word文档中插入书签制成模板,然后程序使用OpenXM ...

随机推荐

  1. Js通用验证

    //-----------------------------------------------------js 验证封装 zhy2014-07-10------------------------ ...

  2. 【转】Linux 查看CPU信息、机器型号等硬件信息

    测试机器的硬件信息: 查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c       8  Intel(R) Xeo ...

  3. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  4. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

  5. RatingBar android:isIndicator="true"

    有时候我们用RatingBar只须要显示不让它选择或改变,解决办法是设置属性 android:isIndicator="true" isIndicator的意思是:是否是指示器,如 ...

  6. spring web中的filter

    昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...

  7. localStorage 读&&写

    localStorage.setItem('edit',nowedit);  写 var nowedit1= localStorage.getItem('editdel');读

  8. Redis学习和应用记录(1)--介绍和安装

    Redis是一个开源的分布式缓存框架,它也常被理解为数据结构服务器,因为它包含丰富的数据类型,如strings, hashes, lists, sets, sorted sets, bitmaps a ...

  9. [Swift通天遁地]三、手势与图表-(5)创建带有标题、图例、坐标轴的柱形图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. akka设计模式系列-消息模型

    通过前面的文章我们总结了几个常见的actor设计模式,但此处不得不提前介绍一下在Akka中消息的设计模式.随着对Akka的使用,我们会发现,使用Akka设计系统其实就是面向消息编程.actor之间消息 ...