题目链接:http://poj.org/problem?id=2533

Time Limit: 2000MS Memory Limit: 65536K

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1a2, ..., aN) be any sequence (ai1ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

题意:

给定长度为 $N$ 的一串整数序列 $a[1 \sim N]$,求其最长上升子序列的长度。

注意:子序列可以不连续,要求严格单增。

题解:

$O(n \log n)$ 解法——贪心+二分。

构建一个栈 $S$ 和一个变量 $top$ 代表栈顶位置,该栈的代表:栈中的第 $i$ 个数 $S[i]$,是序列 $a$ 中,长度为 $i$ 的递增子序列的末尾元素。

初始化 S[top=]=a[] ,即将第一个数字入栈;这很好理解,到目前为止 $a[1]$ 自己是一个长度为 $1$ 的递增子序列。

遍历 $a[ i = 2 \sim N ]$:每次对于 $a[i]$,找出栈 $S[1 \sim top]$ 中第一个大于等于 $a[i]$ 的数的位置 $pos$,若不存在则返回 $pos=top+1$。

这是由于,若存在第一个大于等于 $a[i]$ 的数 $S[pos]$ ,说明对于长度为 $pos$ 的递增子序列,可以用 $a[i]$ 代替掉其原来的末尾元素 $S[pos]$,这样一来,依然是一个长度为 $pos$ 的递增子序列,而且该递增子序列被进一步“加长”的潜力增加。而如果栈中不存在大于等于 $a[i]$ 的数,这说明我可以在目前长度为 $top$ 的递增子序列后面加上一个 $a[i]$,那么我们就得到了一个以 $a[i]$ 为结尾的,长度为 $top+1$ 的递增子序列。

因此,我们把 $S[pos]$ 更新为 $a[i]$,并且尝试更新栈的大小 if(pos>top) top=pos;  。

由于栈 $S$ 中元素始终保持单调递增(而且栈内元素互不相等),所以找 $S$ 中第一个大于等于 $a[i]$ 的数可以使用二分查找。

AC代码(在OpenJudge百练提交):

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+; int n;
vector<int> a; int S[maxn],top;
int LIS(const vector<int>& a)
{
S[top=]=a[];
for(int i=;i<a.size();i++)
{
int pos=lower_bound(S,S+top+,a[i])-S;
S[pos]=a[i], top=max(top,pos);
}
return top+;
} int main()
{
cin>>n;
while(n--)
{
int x; cin>>x;
a.push_back(x);
}
cout<<LIS(a)<<endl;
}

PS.我们可以看到,求第一个大于等于 $a[i]$ 的数使用了lower_bound,相应的如果我们使用upper_bound会怎么样呢?不难证明,我们将会得到最长不下降子序列的长度。

POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]的更多相关文章

  1. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  2. poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

  3. POJ 2533 Longest Ordered Subsequence 最长递增序列

      Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

  4. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  5. POJ 2533 Longest Ordered Subsequence(裸LIS)

    传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  6. Poj 2533 Longest Ordered Subsequence(LIS)

    一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

  7. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

  8. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  9. POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)

    传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...

随机推荐

  1. (三)微信小程序首页的分类功能和搜索功能的实现笔记

    就在昨天,微信宣布了微信小程序开发者工具新增“云开发”功能 下载最新的开发者工具,现在无需服务器即可实现小程序的快速迭代! 分类功能和搜索功能的效果图 1.首页分类功能的实现 boxtwo方法(.js ...

  2. js打印WEB页面内容代码大全

    第一种方法:指定不打印区域 使用CSS,定义一个.noprint的class,将不打印的内容放入这个class内. 详细如下: <style media=print type="tex ...

  3. [51nod1965]奇怪的式子

    noteskey 怎么说,魔性的题目...拿来练手 min_25 正好...吧 首先就是把式子拆开来算贡献嘛 \[ANS=\prod_{i=1}^n \sigma_0(i)^{\mu(i)} \pro ...

  4. Lua中的语句

    [赋值] 赋值的基本含义是修改一个变量或一个table中字段的值,这个和其它语言没有多少区别,但是对于Lua,有一个特性,它允许“多重赋值”,也就是一下子将多个值赋予多个变量,例如以下代码: , pr ...

  5. nginx问题相关记录

    nginx目前主要用来做反向代理和负载均衡,其实它也可以是一个web服务器: 1.反向代理: location /api/ { proxy_next_upstream error timeout ht ...

  6. [HAOI2015]树上操作-树链剖分

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+5; #define mid ((l+r)>> ...

  7. 【原创】大数据基础之Oozie vs Azkaban

    概括 Azkaban是一个非常轻量的开源调度框架,适合二次开发,但是无法直接用于生产环境,存在致命缺陷(比如AzkabanWebServer是单点,1年多时间没有修复),在一些情景下的行为简单粗暴(比 ...

  8. 【原创】大数据基础之Zookeeper(1)介绍、安装及使用

    zookeeper3.4.11 http://zookeeper.apache.org/ 一 简介 ZooKeeper is a centralized service for maintaining ...

  9. Java面试题复习笔记(数据库)

    1.数据库分类? 关系型数据库和非关系型. 常用关系型:Myspl.Oracle.SQLServer 非关系型:Redis.Hadoop.Memcache.Mogobd 2.关系数据库三范式? 范式就 ...

  10. linux服务安装与配置(二):安装xinetd服务

    linux服务安装与配置(二):安装xinetd服务 xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,又叫超级Internet服务器.经常用 ...