POJ2533——Longest Ordered Subsequence(简单的DP)

Longest Ordered Subsequence

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

题目大意:

    给定一个数列,输出其最长递增子序列的长度。

解题思路:

    水题,不解释。

Code:

 1 /*************************************************************************
 2     > File Name: poj2533.cpp
 3     > Author: Enumz
 4     > Mail: [email protected]
 5     > Created Time: 2014年10月27日 星期一 20时07分22秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 1001
23 using namespace std;
24 int date[MAXN];
25 int dp[MAXN];
26 int main()
27 {
28     int N;
29     while (cin>>N)
30     {
31         for (int i=1;i<=N;i++)
32             cin>>date[i];
33         for (int i=1;i<=MAXN-1;i++)
34             dp[i]=1;
35         int Max=1;
36         for (int i=2;i<=N;i++)
37         {
38             for (int j=1;j<=i-1;j++)
39                 if (date[j]<date[i])
40                     dp[i]=max(dp[i],dp[j]+1);
41             Max=max(Max,dp[i]);
42         }
43         cout<<Max<<endl;
44     }
45     return 0;
46 }
时间: 2024-12-10 22:32:00

POJ2533——Longest Ordered Subsequence(简单的DP)的相关文章

POJ-2533 Longest Ordered Subsequence ( DP )

题目链接: http://poj.org/problem?id=2533 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 < ..

poj-2533 Longest Ordered Subsequence 【最长上升子序列】

题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35929   Accepted: 15778 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the

POJ2533 Longest Ordered Subsequence 【最长递增子序列】

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Accepted: 14093 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

poj-2533 longest ordered subsequence(动态规划)

Time limit2000 ms Memory limit65536 kB 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 < ... < i

poj 2533 Longest Ordered Subsequence(线性dp)

题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定义状态dp[i]为以在所有的递增子序列中以A[i]为递增子序列的最后一个数字的所有递增子序列中的最大长度: 如:根据题目,在所有的以3结尾的递增子序列有[3]和[1, 3],所以dp[2] =2; 2)状态转移方程:因为当A[j] < A[i]时(0<= j < i),dp[i] = Max

POJ-2533.Longest Ordered Subsequence (LIS模版题)

本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 1e3 + 5; 7 int a[maxn], dp[m

Longest Ordered Subsequence POJ - 2533 dp 最长上升/不下降 子序列

#include<iostream> using namespace std ; const int N=1010; int f[N]; int a[N]; int n; int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++) { f[i]=1; for(int j=1; j<=i; j++) { if(a[j]<a[i]) { f[i]=ma

poj 2533 Longest Ordered Subsequence(dp)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36159   Accepted: 15882 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

POJ 2533 Longest Ordered Subsequence(dp LIS)

Language: Default Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33986   Accepted: 14892 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric seq