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. javascript 中遍历数组的简单方法

    在Javascript中有自带方便遍历数组的方法(此方法非彼方法不要误会哦): 1 .利用for( index in array ){}; 2.利用 array.forEach( function(e ...

  2. python3基础视频教程

    随着目前Python行业的薪资水平越来越高,很多人想加入该行业拿高薪.有没有想通过视频教程入门的同学们?这份Python教程全集等你来学习啦! python3基础视频教程:http://pan.bai ...

  3. Vue.js优雅的实现列表清单

        一.Vue.js简要说明 Vue.js (读音 /vjuː/) 是一套构建用户界面的渐进式框架.与前端框架Angular一样, Vue.js在设计上采用MVVM模式,当View视图层发生变化时 ...

  4. 利用C#实现分布式数据库查询

    随着传统的数据库.计算机网络和数字通信技术的飞速发展,以数据分布存储和分布处理为主要特征的分布式数据库系统的研究和开发越来越受到人们的关注.但由于其开发较为复杂,在一定程度上制约了它的发展.基于此,本 ...

  5. 删除一个大表导致其他表Opening tables

  6. STM32F4中USB与PC双向通信

    STM32F4系列处理器内部集成了USB-OTG控制器,在不要外部USB IC下就可以实现USB通信,最近两天看了下如何利用STM32的USB实现通信,记录下关键步骤: 1. 从http://www. ...

  7. Android设置屏幕方向

    设置方法:在AndroidManifest.xml中的Activity里加一个属性android:screenOrientation.例如设置该Activity为横向 <activity and ...

  8. html基本标签与属性

    HTML 超文本标记语言 html5 建立一个HTML文件:文件名 . 后缀(html)   解析:就是去识别 注释:就是给开发人员开的批注------浏览器不去解析(不去输出)   HTML的整体框 ...

  9. 【Python】三个例子教你写代码

    这篇文章包括用Python编写的斐波那契数列,三位数的水仙花数和百钱买百鸡的基础代码: (一)斐波那契数列: ''' def hanshu(n): n_1 = 1 n_2 = 1 m = n sumn ...

  10. windows下tensorflow的安装

    一.直接python安装 1.CPU版本: pip3 install --upgrade tensorflow 2.GPU版本:pip3 install --upgrade tensorflow-gp ...