LIS的nlogn
nlogn老忘,开个帖记录一下
开一个栈,每次取栈顶元素top和读到的元素temp做比较,如果temp > top 则将temp入栈;如果temp < top则二分查找栈中的比temp大的第1个数,并用temp替换它。 最长序列长度即为栈的大小top。
例如对2 1 5 3 6 4 8 9 7
每一步的结果
1. 2
2. 1
3. 1 5
4. 1 3
5. 1 3 6
6. 1 3 4
7. 1 3 4 8
8. 1 3 4 8 9
9. 1 3 4 7 9
最后的13479并不是LIS,而是对应LIS长度的最小末尾,记录栈顶元素即可求出对应LIS序列
const int MAXN=;
int a[MAXN],b[MAXN];
//用二分查找的方法找到一个位置,使得num>b[i-1] 并且num<b[i],并用num代替b[i]
int Search(int num,int low,int high)
{
int mid;
while(low<=high)
{
mid=(low+high)/;
if(num>=b[mid]) low=mid+;
else high=mid-;
}
return low;
}
int DP(int n)
{
int i,len,pos;
b[]=a[];
len=;
for(i=;i<=n;i++)
{
if(a[i]>=b[len])//如果a[i]比b[]数组中最大还大直接插入到后面即可
{
len=len+;
b[len]=a[i];
}
else//用二分的方法在b[]数组中找出第一个比a[i]大的位置并且让a[i]替代这个位置
{
pos=Search(a[i],,len);
b[pos]=a[i];
}
}
return len;
}
LIS的nlogn的更多相关文章
- 最长上升子序列(LIS)nlogn模板
参考https://www.cnblogs.com/yuelian/p/8745807.html 注意最长上升子序列用lower_bound,最长不下降子序列用upper_bound 比如123458 ...
- POJ 1631 Bridging signals(LIS O(nlogn)算法)
Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; ...
- 算法描述》关于LIS的nlogn方法
上次TYVJ有一道裸LIS,然而我当时直接打了一个N^2暴力就草草了事,然后就ZZ了,只拿了60分,其实NlogN的LIS和N^2的差的不多,只是没有N^2,好想罢了,鉴于某学弟的要求,所以就重现一下 ...
- ACdream 1216——Beautiful People——————【二维LIS,nlogn处理】
Beautiful People Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (J ...
- BZOJ 1609 [Usaco2008 Feb]Eating Together麻烦的聚餐:LIS & LDS (nlogn)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1609 题意: 给你一个只由数字"1,2,3"组成的序列a[i],共n个 ...
- hdu4352 XHXJ's LIS[数位DP套状压DP+LIS$O(nlogn)$]
统计$[L,R]$内LIS长度为$k$的数的个数,$Q \le 10000,L,R < 2^{63}-1,k \le 10$. 首先肯定是数位DP.然后考虑怎么做这个dp.如果把$k$记录到状态 ...
- hdu 1950 最长上升子序列(lis) nlogn算法【dp】
这个博客说的已经很好了.http://blog.csdn.net/shuangde800/article/details/7474903 简单记录一下自己学的: 问题就是求一个数列最长上升子序列的长度 ...
- LIS(nlogn)算法描述//线性DP经典类型
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
随机推荐
- HDFS原理介绍
HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Googl ...
- poj 1035
http://poj.org/problem?id=1035 poj的一道字符串的水题,不难,但就是细节问题我也wa了几次 题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出 ...
- Qt字符转换
1.QString -> char* #include<QTextCodec> QTextCodec::setCodecForLocale(QTextCodec::codecFor ...
- delphi 快捷键
1. 编辑器 按键时候请注意输入法: 英文状态 Code Folding (Fold ---Methods) [Ctrl + Shift] + K + M
- 一些笔试题(C/C++)
1.there are two variables, don't use if.. else or ?: or switch or other judgement statements,find ou ...
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- Django~Models2
Generally, each model maps to a single database table. Each attribute of the model represents a data ...
- Effective C++ -----条款21:必须返回对象时,别妄想返回其reference
绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...
- Velocity 基本语法
Velocity 基本语法 Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言可以使用在 Java 中定义的对象和变量上.Velocity 是 Apache 基金会的项目,开发的目 ...
- javascript 操作cookie
function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDa ...