记录一下,O(nlgn)的算法求LIS

 //HHH
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MX 1005 int num[MX];
int dp[MX];
int high[MX]; // 长为i的最小大小 int bi_search(int x,int h)
{
int l=,r=h;
int res=;
while (l<=r)
{
int mid = (l+r)/;
if (x>high[mid]) //不减序列改为 x>=high[mid]
{
res=mid;
l=mid+;
}
else r=mid-;
}
return res+;
} int calc(int l,int r)
{
if (l>r) return ;
memset(high,,sizeof(high)); //要为最界限值
int h=;
dp[l]=;
high[]=num[l];
for (int i=l+;i<=r;i++)
{
if (num[i]>high[h]) //不减序列改为 x>=high[mid]
{
h++;
dp[i]=h;
high[h]=num[i];
}
else
{
int p=bi_search(num[i],h);
dp[i]=p;
high[p]=num[i];
}
}
return h;
} int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&num[i]);
printf("%d\n",calc(,n));
}
return ;
}

LIS(模板)的更多相关文章

  1. nlogn LIS模板

    nlogn 模板 最长上升 #include<bits/stdc++.h> using namespace std; ; int n,x,y,a[N],num[N],d[N],len; / ...

  2. 【模板】LIS模板 洛谷P1091 [NOIP2004提高组]合唱队形 [2017年4月计划 动态规划11]

    以题写模板. 写了两个:n^2版本与nlogn版本 P1091 合唱队形 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队 ...

  3. 最长上升子序列(LIS)模板

    最长递增(上升)子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增(上升)子序列. 考虑两个数a[x ...

  4. poj 3903 poj 2533 (LIS模板题)

    pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. Sample Input 6 5 2 1 4 5 3 3 1 1 1 ...

  5. 求最长上升子序列(Lis模板)

    实现过程 定义已知序列数组为dp[]:dp[1…8]=389,207,155,300,299,170,158,65 我们定义一个序列B,然后令 i = 1 to 8 逐个考察这个序列.此外,我们用一个 ...

  6. 二分LIS模板

    假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5. 下面一步一步试着找出它. 我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列. ...

  7. 动态规划-最长上升子序列(LIS模板)多解+变形

    问题描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列( ...

  8. POJ1631 LIS模板

    题目:http://poj.org/problem?id=1631 两种nlogn的方法. 1.树状数组优化暴力.有种扫描线的感觉,以时间保证位置,把值作为数组脚标. 2.记录长为...的上升子序列末 ...

  9. 树状数组求LIS模板

    如果数组元素较大,需要离散化. #include <iostream> #include <cstdio> #include <cstring> #include ...

随机推荐

  1. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)人机界面Paintbuffer Overflow怎么办

    当在界面上绘制了太多元素时,点击运行会在左上角弹出对话框提示内容容量不够   在英文版的说明中,点击Visualization Manager就可以进行设置     更多教学视频和资料下载,欢迎关注以 ...

  2. Mysql 中文中繁杂的字 插入报错的 解决方案

    首先 数据库默认编码选用 utf8 连接字符串也相应改成utf8,不能是gb2312

  3. ubuntu14.10环境下使用apache2.2配置代理服务器

    参考网址: 使用Apache架设代理服务器 ubuntu之apache正向代理及反向代理(ProxyPass\ProxyPassReverse) Apache配置正向代理与反向代理 我的配置步骤: 1 ...

  4. Selenium webdriver Java firefox 路径设置问题

    问题: Cannot find firefox binary in PATH. Make sure firefox is installed. 原因:selenium找不到Firefox浏览器. 方法 ...

  5. Selenium webdriver Java 开始

    最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...

  6. Instant Messaging for OpenERP v7

    This module is based on the socket.io real time implementation. It inherit Of the modules web_longpo ...

  7. linux如何手动释放linux内存

    当在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching.这个问题,貌似有不少人在问,不过都没有看到有什么很好解决的办法.那么我来谈谈这个问题 ...

  8. MongoDB查询经典方式

    原文地址:http://www.cnblogs.com/stephen-liu74/archive/2012/08/03/2553803.html 1.  基本查询:    构造查询数据.    &g ...

  9. 字符串函数---strcmp()与strncmp()具体解释及实现

    一.strcmp()与strncmp() strcmp():strcmp(s1,s2);            比較两个字符串.        strncmp():strncmp(s1,s2);   ...

  10. php类库PHP QR Code 二维码

    php类库PHP QR Code 二维码 php类库PHP QR Code 二维码 php类库PHP QR CodePHP QR Code is open source (LGPL) library ...