zoukankan      html  css  js  c++  java
  • POJ2533:Longest Ordered Subsequence

    Longest Ordered Subsequence
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 37454   Accepted: 16463

    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

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

    比较好理解的一道动态规划。题意是求一个序列中最长的递增序列的长度,就是逐渐输入逐渐比对,如果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;
    }




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    vector
    codeforces 1453D. Checkpoints
    [ICPC2019 WF]Hobson's Trains
    [ICPC2019 WF]Circular DNA
    计算几何板子
    CSP-S2020 贪吃蛇(洛谷民间数据)
    CSP-S2020 函数调用(洛谷民间数据)
    [NOI Online #3 提高组]魔法值
    [NOI Online #1 提高组]冒泡排序
    佳能m62套机5500 佳能EOS M50 M6 MARK2 II二代 最低到过5800
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785886.html
Copyright © 2011-2022 走看看