ACM/ICPC Judge Online of HUNAN UNIVERSITY (hnu.edu.cn)
Longest Ordered Subsequence |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 4227, Accepted users: 3726 |
Problem 10001 : No special judgement |
Problem 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 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 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 |
Problem Source |
HNU Contest |
一眼动态规划,但是dp数组的意义不是很好想,
dp[i]代表的是以第i+1个数字结尾的最长递增子序列的长度。
易得状态转移方程如下:
dp[i]=max(dp[i],dp[j]+1) (j从0~i)
AC代码:
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int list[N];
int res[N];
for (int j = 0; j < N; j++) {
cin >> list[j];
res[j] = 1;
}
res[0] = 1;
int r = 0;
for (int i = 1; i < N; i++) {
for (int j = 0; j < i; j++) {
if (list[i] > list[j]) {
res[i] = max(res[i], res[j] + 1);
r = max(r, res[i]);
}
}
}
cout << res[N-1];
return 0;
}