poj2533 最长升序子序列解题报告

Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23388   Accepted: 10113

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



这是一道经典的dp题,对于初学者可以多思考思考,对了解动态规划有很大帮助,同时这道题也有O(n²)以及O(nlog)两种复杂度,有兴趣的话可以多加研究。
我的代码如下:

#include
<iostream> using namespace std; int a[1005],b[1005],i,j,m,maxl;//a[]是给定数组,b[i]保存从第一个数到第i个数的最长升序子序列长度,maxl保存当前最长的升序子序列长度 int main() { cin>>m;maxl=1; for(i=0;i<m;i++) { cin>>a[i];b[i]=1;//初始化 b[i]置1是因为对于每一个单独的数,其长度为1 } for(i=1;i<m;i++)//对每个数进行枚举 { for(j=0;j<=i;j++) if(a[i]>a[j]){b[i]=max(b[i],b[j]+1);}//b[j]+1是当前长度加1
maxl=max(maxl,b[i]);//用maxl保存当前最长的长度 } cout<<maxl<<endl; }

posted on 2012-07-21 11:16  Blue-Sea  阅读(115)  评论(2)    收藏  举报

导航