您的位置:首页 > 产品设计 > UI/UE

POJ2533:Longest Ordered Subsequence

2015-06-11 13:37 260 查看
Longest Ordered Subsequence

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 37454Accepted: 16463
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


这道题自己一开始就是这个思路,只不过就在想时间会不会超。

比较好理解的一道动态规划。题意是求一个序列中最长的递增序列的长度,就是逐渐输入逐渐比对,如果value[j]>value[i]的话,dp[j]=max(dp[0],dp[1].....dp[j-1])+1。

代码:

#include <iostream>
using namespace std;

int value[1005];
int dp[1005];

int main()
{
	int num;
	int i,j,max;

	cin>>num;

	for(i=0;i<num;i++)
	{
		cin>>value[i];
		dp[i]=1;
		max=1;
		for(j=0;j<i;j++)
		{
			if(value[i]>value[j])
			{
				if(dp[j]+1>max)
				{
					max=dp[j]+1;
				}
			}
		}
		dp[i]=max;
	}
	
	max=1;
	for(i=0;i<num;i++)
	{
		if(dp[i]>max)
		{
			max=dp[i];
		}
	}
	cout<<max<<endl;
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: