Title:

http://poj.org/problem?id=2533

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
这个是经典的动态规划题,像我这种渣渣,连这个都要看好久,好吧,其实简单的DP还好,就是nlogn复杂度的,看了好久。嗯,但是呢,网上的那些代码都没有我写的简洁,好吧,这也是聊以自慰呢。。
思路(1): O(n^2)

令A[i]表示输入第i个元素,D[i]表示从A[1]到A[i]中以A[i]结尾的最长子序列长度。对于任意的0 <  j <= i-1,如果A(j) < A(i),则A(i)可以接在A(j)后面形成一个以A(i)结尾的新的最长上升子序列。对于所有的 0 <  j <= i-1,我们需要找出其中的最大值。

DP状态转移方程:

D[i] = max{1, D[j] + 1} (j = 1, 2, 3, ..., i-1 且 A[j] < A[i])

解释一下这个方程,i, j在范围内:

如果 A[j] < A[i] ,则D[i] = D[j] + 1

如果 A[j] >= A[i] ,则D[i] = 1

int main(){
int a[SIZE];
int d[SIZE];
int n;
cin>>n;
for (int i = ; i < n; i++)
cin>>a[i];
int m = INT_MIN;
for (int i = ;i < n; i++){
d[i] = ;
for (int j = ; j < i; j++){
if (a[j] < a[i])
d[i] = max(d[i],d[j]+);
}
m = max(m,d[i]);
}
cout<<m<<endl;
//system("pause");
}

(2)nlogn的解法。

重新定义下dp[i]

dp[i] 的意思是所有长度为i+1的LIS中末尾元素的最小值

那么最开始,dp[0] = a[0]

因为dp是一个有序数组,所以每次我们都去这个数组中寻找a[i]的位置,例如dp = {2,4,6},如果a[i] = 5,那么a[i]的位置应该是4~6之间,所以返回index = 2.怎么理解呢,dp[i]的定义!所以a[i] = 5,那么有两个长度的最小值都比a[i]小,那加入a[i],这个长度肯定就是3了,然后这个index=2,同时,比较5和6,我们要选择最小的值。我的搜索代码包含了边界的情况,因此,在主函数中就不需要判断。

int search(int * s, int t,int l, int r){
while (l <= r){
int m = (l + r)/;
if (s[m] == t){
return m;
}else if (s[m] < t){
l++;
}else{
r--;
}
}
return l;
}
int main(){
int a[SIZE];
int n;
cin>>n;
for (int i = ; i < n; i++){
cin>>a[i];
}
int stack[SIZE]; fill(stack,stack+SIZE,INT_MAX);
stack[] = a[];
int cur_index = ;
for (int i = ; i < n; i++){
int j = search(stack,a[i],,cur_index);
stack[j] = min(stack[j],a[i]);
if (j == cur_index)
cur_index++;
}
cout<<cur_index;
//system("pause");
}
												

POJ:最长上升子序列的更多相关文章

  1. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence

    1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...

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

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

  3. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  4. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  6. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  7. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  8. [poj 1533]最长上升子序列nlogn树状数组

    题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...

  9. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

随机推荐

  1. WCF 基础

    ServiceModel 配置元素 Binding 配置元素: 客户端Web.config: <?xml version="1.0" encoding="utf-8 ...

  2. 你真的知道css三种存在样式(外联样式、内部样式、内联样式)的区别吗?

    css样式在html中有三种存在形态: 内联样式:<div style="display: none"></div> 内部样式: <style> ...

  3. 【WCF--初入江湖】04 WCF通信模式

    04 WCF通信模式 WCF的通信模式有三种 [1]请求响应模式: 只能是客户端调用服务器; 客户端请求并等待服务器的响应后才继续执行后续操作(异步调用除外) [2]单工模式: 只能是客户端调用服务器 ...

  4. 【MongoDb--初入江湖】windows下安装MongoDb

    一.windows下安装MongoDb http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

  5. 仪表盘 hostmap 新玩法让运维工作越玩越 high

    Cloud Insight 第13次新品发布会现在开始,首先非常感谢大家前来看我们的新功能发布会,下面我先给大家介绍一下新功能,之后有什么问题大家尽管问

  6. sublime text3 配置插件包记录

    前言: 很多插件已经开始放弃支持ST2了,所以推荐使用ST3,大量的最新插件和最新功能已经不再支持st2了. 下载地址戳这里:http://www.sublimetext.com/3 1.所有插件 易 ...

  7. js 后台异步执行

    public void AlertMsg(string msg, bool async) { string script = string.Format("alert('{0}'); &qu ...

  8. TopCoder 649 div1 & div2

    最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...

  9. 如何正确学习JavaScript

    不要这样学习JavaScript 不要一开始就埋头在成堆的JavaScript在线教程里 ,这是最糟糕的学习方法.或许在看过无数个教程后会有点成效,但这样不分层次结构地学习一个东西实在是十分低效,在实 ...

  10. Android中XML格式数据的简单使用

    源码: package com.wangzhu.demo; import java.io.IOException; import java.io.StringWriter; import javax. ...