Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 38980   Accepted: 17119

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

Source

Northeastern Europe 2002, Far-Eastern Subregion



n*n算法


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std; int n;
int a[1001];
int dp[1010]; int main()
{
while(scanf("%d",&n)!=EOF)
{
int maxx = -1;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
dp[i] = 1;
for(int j=0;j<i;j++)
{
if(a[i]>a[j] && dp[j]+1>dp[i])
{
dp[i] = dp[j] + 1;
}
}
if(maxx < dp[i])
{
maxx = dp[i];
}
}
printf("%d\n",maxx);
}
return 0;
}



n*logn算法:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define inf 999999 using namespace std; int n;
int dp[1010];
int a[1010]; int res(int len,int num)
{
int l = 0,r = len;
while(l!=r)
{
int mid = (l+r)>>1;
if(dp[mid] == num)
{
return mid;
}
else if(dp[mid]<num)
{
l = mid + 1;
}
else if(dp[mid]>num)
{
r = mid;
}
}
return l;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int len = 1;
dp[0] = -1;
for(int i=1;i<=n;i++)
{
dp[i] = inf;
int k = res(len,a[i]);
if(k == len)
{
len++;
}
dp[k] = a[i];
}
printf("%d\n",len-1);
}
return 0;
}

POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)的更多相关文章

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

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

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

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

  3. POJ - 2533 Longest Ordered Subsequence(最长上升子序列)

    d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...

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

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

  5. poj 2533 Longest Ordered Subsequence(dp)

    题目:http://poj.org/problem?id=2533 题意:最长上升子序列.... 以前做过,课本上的思想 #include<iostream> #include<cs ...

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

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

  7. POJ 2533 Longest Ordered Subsequence(裸LIS)

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

  8. 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)

    Longest Ordered Subsequence 搬中文 Descriptions: 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序 ...

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

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

随机推荐

  1. Lambda转sql部分代码保存

    public class SqlExpressionTree { public string GetQuerySql<T>(Expression<Func<T, bool> ...

  2. 搭建本地Tomcat

    1.下载自己需要的版本我安装的Tomcat8(https://tomcat.apache.org/). 2.根据自己的电脑下载需要的系统版本.我的电脑是Windows64位的操作系统 3.选择一个目录 ...

  3. ThinkPHP模版验证要注意的地方

    Model页面 <?php class LoginModel extends Model { //protected $tableName = 'userinfo'; //表名和model不一致 ...

  4. SpringMVC处理ajax请求的注意事项

    .首先要知道ajax请求的核心是JavaScrip对象和XmlHttpRequest,而浏览器请求的核心是浏览器 ajax请求 浏览器请求 场景一:使用ajax获取session中的user 从上图可 ...

  5. 腾讯课堂web零基础

    utf是国际编码 gb2312 国人发明的 gbk 补充集 想看网站源代码可以按F12 <meta name ='keywords' content='设置关键字'> <meta n ...

  6. Winwos Server 2012发布ASP.NET MVC5 项目

    一.本文实验环境: Windows Server 2012 R2 Visual Studio 2015 项目为:ASP.NET MVC 5.0,使用的是SQL SERVER 2008 R2数据库 二. ...

  7. 【设计模式系列】之OO面向对象设计七大原则

    1  概述 本章叙述面向向对象设计的七大原则,七大原则分为:单一职责原则.开闭原则.里氏替换原则.依赖倒置原则.接口隔离原则.合成/聚合复用原则.迪米特法则. 2  七大OO面向对象设计 2.1 单一 ...

  8. Servlet之初始化参数和传递数据(ServletConfig,ServletContext )

    ServletConfig 容器初始化一个Servlet的时候,会为这个Servlet建一个唯一的Servletconfig的对象(Servlet的配置对象) 容器会从部署的描述文件(web.xml) ...

  9. ACM个人零散知识点整理

    ACM个人零散知识点整理 杂项: 1.输入输出外挂 //读入优化 int 整数 inline int read(){ int x=0,f=1; char ch=getchar(); while(ch& ...

  10. zzuli 1812: sort 排序

    1812: sort Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 352  Solved: 216 SubmitStatusWeb Board De ...