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. ognl.NoSuchPropertyException(没有对应属性异常)

    ognl.NoSuchPropertyException: com.xie.struts2.tags.modal.Student.sName(没有对应属性异常) at ognl.ObjectPrope ...

  2. 将HTML格式的String转化为HTMLElement

    代码如下: <meta charset="UTF-8"> <title>Insert title here</title> </head& ...

  3. Lambda表达式 之 C#

    Lambda表达式 "Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量.它可以包含表达式和语句,并且可用于创建委托 ...

  4. Even Tree

    Link: https://www.hackerrank.com/challenges/even-tree def search(a,b): # 根据核心算法和题目要求要筛选边 seen = {} s ...

  5. 数值运算内建函数(core python programming 2nd edition 5.6.2)

    数值运算内建函数 函数  功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...

  6. 了不起的分支和循环01 - 零基础入门学习Python007

    了不起的分支和循环01 让编程改变世界 Change the world by program 我们今天的主题是"了不起的分支和循环",为什么小甲鱼不说C语言,不说Python了不 ...

  7. [TYVJ] P1002 谁拿了最多奖学金

    谁拿了最多奖学金 背景 Background NOIP2005复赛提高组第一题   描述 Description 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同 ...

  8. [科普]MinGW vs MinGW-W64及其它

    转载:http://tieba.baidu.com/p/3186234212?pid=54372018139&cid=#54372018139 这里也转一下吧. 部分参照备忘录原文: bitb ...

  9. java设计模式--结构型模式--装饰模式

    装饰模式 概述 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. 适用性 1.在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. 2.处理 ...

  10. poj 1149 pigs(最大流)

    题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...