记录一下,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. javascript - 你不容错过的es6模板写法

    /** * ``即重音符(128键盘左上角ESC下面那个键盘) * 隶属:模板字符串 */ let unit = '4'; let keywords = 'uc'; // step1:模板变量 ${v ...

  2. 使用UnityVS1.8.2搭配VS2013进行Unity3D代码调试(下载+安装+使用+问题解决)

    http://blog.dou.li/UnityVS1_8_2-VS2013.html 备整合UnityVS1.8.2和VS2013调试Unity3D代码,安装后会有些问题无法调试,详情继续往下看. ...

  3. 【 D3.js 入门系列 --- 5 】 怎样加入坐标轴

    本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处.谢谢.      第3节中做了一个图标.但没有为它加入一个对应的坐标轴. ...

  4. Srping AOP xml方式

      使用aop需要: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...

  5. HTTP请求报文属性详解

    HTTP请求报文组成:请求行+请求头+请求体 注意:请求体和URL都可以传递请求参数. 常见请求头属性: 1.Accept  作用:浏览器客户端用来告诉服务端能接受什么类型的响应. 例如:  Acce ...

  6. centos7安装thrift

    1. 升级所有软件包 yum -y update 2.安装开发工具 yum -y groupinstall "Development Tools" 3.安装wget yum -y ...

  7. jQuery框架源码解读

    1.jQuery 1.9.1 parseJSON: function( data ) { // Attempt to parse using the native JSON parser first ...

  8. 单例模式获取JDBC连接

    package com.jdbc.test; import java.io.IOException; import java.io.InputStream; import java.sql.Conne ...

  9. 【Scala】使用Option、Some、None,避免使用null

    避免null使用 大多数语言都有一个特殊的关键字或者对象来表示一个对象引用的是"无",在Java,它是null.在Java 里,null 是一个关键字,不是一个对象,所以对它调用不 ...

  10. MVC Controller return 格式之JsonResult、ContentResult、RedirectResult……

      //语法 public class JsonResult : ActionResult public class ContentResult : ActionResult public class ...