记录一下,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. Python学习笔记(四)多进程的使用

    python中多进程与Linux 下的C基本相同.   fork的基本使用   先看最简单的例子: # coding: utf-8 import os def my_fork(): pid = os. ...

  2. UICollectionViews有了简单的重排功能

    代码地址如下:http://www.demodashi.com/demo/13213.html 一.前言 我是UICollectionView的忠实粉丝.这个类比起它的老哥UITableView类具有 ...

  3. html 5 中的 6位 十六进制颜色码 代表的意思180313

    人的眼睛看到的颜色有两种: ⒈ 一种是发光体发出的颜色,比如计算机显示器屏幕显示的颜色: ⒉ 另一种是物体本身不发光,而是反射的光产生   的颜色,比如看报纸和杂志上的颜色. 我们又知道任何颜色都是由 ...

  4. linux 查看cpu相关信息命令

  5. 设计模式之代理模式(php实现)

    github地址:https://github.com/ZQCard/design_pattern /** * 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口. * 1.Window ...

  6. [JavaSecurity] - AES Encryption

    1. AES Algorithm The Advanced Encryption Standard (AES), also as known as Rijndael (its original nam ...

  7. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  8. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  9. java - day15 - nstInner

    匿名内部类 package com.javatest.mama; public class Mama { int x = 5; public static void main(String[] arg ...

  10. android studio中文乱码问题

    在build.gradle中加入代码: tasks.withType(JavaCompile) { options.encoding = "UTF-8" }