LIS算法
LIS(Longest Increasing Subsequence)最长上升(不下降)子序列。
1. O(n^2)
#include<cstdio>
#include<algorithm>
using namespace std; int n,ans;
int a[],d[]; int main(){
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%d",&a[i]);
for(int i=;i<=n;++i){
d[i]=;
for(int j=;j<i;++j)
if(a[i]>a[j]&&d[i]+>d[j])d[i]=d[j]+;
}
for(int i=;i<=n;++i)ans=max(ans,d[i]);
printf("%d",ans);
return ;
}
2. O(n logn)
#include<cstdio>
int n;
int a[];
int stack[],top; int main(){
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%d",&a[i]);
stack[++top]=a[];
for(int i=;i<=n;++i){
if(a[i]>stack[top])stack[++top]=a[i];
else {
int l=,r=top;
while(l<=r){
int mid=l+r>>;
if(stack[mid]<a[i])l=mid+;
else r=mid-;
}
stack[r]=a[i];
}
}
printf("%d",top);
return ;
}
LIS算法的更多相关文章
- java实现LIS算法,出操队形问题
假设有序列:2,1,3,5,求一个最长上升子序列就是2,3,5或者1,3,5,长度都为3. LIS算法的思想是: 设存在序列a. ① 如果只有一个元素,那么最长上升子序列的长度为1: ② 如果有两个元 ...
- N种方法妙讲LIS算法
LIS算法经典汇总 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 逐个 ...
- 时间复杂度为O(nlogn)的LIS算法
时间复杂度为 n*logn的LIS算法是用一个stack维护一个最长递增子序列 如果存在 x < y 且 a[x] > a[y],那么我们可以用a[y]去替换a[x] 因为a[y]比较小 ...
- 算法心得1:由$nlogn$复杂度的LIS算法引起的思考
LIS(Longest Increasing Subsequence)是一类典型的动态规划类问题,简化描述如下: 给定$N(n) = \{1,2...,n\}$的一个排列$P(n)$,求$P(n)$中 ...
- POJ 1631 Bridging signals(LIS O(nlogn)算法)
Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...
- 常用查找数据结构及算法(Python实现)
目录 一.基本概念 二.无序表查找 三.有序表查找 3.1 二分查找(Binary Search) 3.2 插值查找 3.3 斐波那契查找 四.线性索引查找 4.1 稠密索引 4.2 分块索引 4.3 ...
- [noip科普]关于LIS和一类可以用树状数组优化的DP
预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...
- HDU 4352 XHXJ's LIS
奇妙的题. 你先得会另外一个nlogn的LIS算法.(我一直只会BIT.....) 然后维护下每个数码作为结尾出现过没有就完了. #include<iostream> #include&l ...
- 486E - LIS of Sequence(LIS)
题意:给一个长度为n的序列.问每一个数关于序列的LIS(longest increasing subsequence)是什么角色. 这里分了三种: 1.此数没有出如今随意一条LIS中 2.此数出如今至 ...
随机推荐
- PAT (Advanced Level) 1054. The Dominant Color (20)
简单题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #inc ...
- C++多线程二
SuspendThread()暂停一个线程,ResumeThread()重启一个线程.参数均为线程的句柄. #include <iostream> #include <windows ...
- Java基于Socket文件传输示例
http://www.blogjava.net/sterning/archive/2007/10/13/152508.html 最近需要进行网络传输大文件,于是对基于socket的文件传输作了一个初步 ...
- Android Guts: Intro to Loopers and Handlers
One of the reasons I love Android API is because it contains so many useful little things. Many of t ...
- 猜数游戏-flag的运用
package my;import java.util.Scanner;public class MyJava { public static void main(String[] ar ...
- PAT (Advanced Level) 1014. Waiting in Line (30)
简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- CodeForces 620E New Year Tree
线段树+位运算 首先对树进行DFS,写出DFS序列,记录下每一个节点控制的区间范围.然后就是区间更新和区间查询了. 某段区间的颜色种类可以用位运算来表示,方便计算. 如果仅有第i种颜色,那么就用十进制 ...
- (译)Windsor入门教程---第四部分 整合
介绍: 目前为止,已经介绍了应用程序的各个部分.首先是添加了Windsor程序集,然后是添加了控制器工厂,还添加了installer类来注册控制器.虽然但是我们还没用在应用程序中调用他们.在这 ...
- (中等) HDU 4370 0 or 1,建模+Dijkstra。
Description Given a n*n matrix C ij (1<=i,j<=n),We want to find a n*n matrix X ij (1<=i,j&l ...
- POJ 2395 Out of Hay
这个问题等价于求最小生成树中权值最大的边. #include<cstdio> #include<cstring> #include<cmath> #include& ...