Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., 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
题意:输出最长递增子序列的长度
思路:直接裸LIS,第一次使用,使用两种方法
 
第一种:复杂度n^2
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int a[1005],dp[1005],n; int LIS()
{
int i,j,ans,m;
dp[1] = 1;
ans = 1;
for(i = 2;i<=n;i++)
{
m = 0;
for(j = 1;j<i;j++)
{
if(dp[j]>m && a[j]<a[i])
m = dp[j];
}
dp[i] = m+1;
if(dp[i]>ans)
ans = dp[i];
}
return ans;
} int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 1;i<=n;i++)
scanf("%d",&a[i]);
printf("%d\n",LIS()); } return 0;
}

第二种:nlogn

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int a[1005],dp[1005],c[1005],n; int bin(int size,int k)
{
int l = 1,r = size;
while(l<=r)
{
int mid = (l+r)/2;
if(k>c[mid] && k<=c[mid+1])
return mid+1;
else if(k<c[mid])
r = mid-1;
else
l = mid+1;
}
} int LIS()
{
int i,j,ans=1;
c[1] = a[1];
dp[1] = 1;
for(i = 2; i<=n; i++)
{
if(a[i]<=c[1])
j = 1;
else if(a[i]>c[ans])
j = ++ans;
else
j = bin(ans,a[i]);
c[j] = a[i];
dp[i] = j;
}
return ans;
} int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
printf("%d\n",LIS()); } return 0;
}
												

POJ2533:Longest Ordered Subsequence(LIS)的更多相关文章

  1. POJ2533:Longest Ordered Subsequence

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37454   Acc ...

  2. poj 2533 Longest Ordered Subsequence(LIS)

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

  3. POJ-2533.Longest Ordered Subsequence (LIS模版题)

    本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...

  4. POJ 2533 Longest Ordered Subsequence LIS O(n*log(n))

    题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #include <algorithm> using ...

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

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

  6. (线性DP LIS)POJ2533 Longest Ordered Subsequence

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 66763   Acc ...

  7. POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  8. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  9. POJ2533——Longest Ordered Subsequence(简单的DP)

    Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...

随机推荐

  1. Java中,&&与&;||与|的区别

    我们很多人在学习Java的时候,或者其他语言(如:C#,.Net等)都会遇到&和&&,|和||.然而,如果你没有真正理解他们的意思,这回给你的思路上带来很大的麻烦.在Java的 ...

  2. Kendo Web UI Grid里时间格式转换

    我和大家分享一下我的kendo的学习心得.如果不好的地方多多包含或者给我留言请看图 kendo里时间格式如图Oder Date列里的格式.但是我们想把时间转换成中国人习惯的格式.如Shipped Da ...

  3. Scala学习笔记--List、ListBuffer

    ListBuffer(列表缓冲) ListBuffer类位于  scala.collection.mutable.ListBuffer val buf = new ListBuffer[Int] va ...

  4. 下一代云计算模式:Docker正掀起个性化商业革命

    作者: 吴宁川  来源: ITValue  发布时间: 2015-09-20 10:41  阅读: 10008 次  推荐: 16   原文链接   [收藏] 文/ITValue 记者吴宁川 从 20 ...

  5. Mysql update 错误

    今天在工作的时候发现自己update 一个表的某个字段超时,想了好久,首先想到的办法是,延长操作时间: mysql> set innodb_lock_wait_timeout=100 mysql ...

  6. mips平台使用jdbc操作sqlite的最终解决方案

    1.概述: 本项目需要在多平台上(mips必须支持)操作嵌入式数据库sqlite,而最新的sqlite-jdbc-3.15.1.jar的本地驱动只含有少量的平台,于是解决支持mips平台实在必行.方法 ...

  7. python之路-随笔 python处理excel文件

    小罗问我怎么从excel中读取数据,然后我百了一番,做下记录 以下代码来源于:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html ...

  8. VBA清除Excelpassword保护,2003/2007/2010均适用

    Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...

  9. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  10. Android软件开发之常用系统控件界面整理

    1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android ...