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
Inputcopy | Outputcopy |
---|---|
7 1 7 3 5 9 4 8 | 4 |
#include<iostream>
using namespace std;
#define MAX 10010
int T;
int arr[MAX] = { 0 }, dp[MAX] = { 0 };
int list(int n) {
int res = 0;
//思想就是以arr[i]为结尾的最长上升子序列
for (int i = 1; i <= n; i++) {
dp[i] = 1;//最坏情况自身为子序列
for (int j = 1; j <= i; j++) {
if (arr[i] > arr[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
res = max(res, dp[i]);
}
return res;
}
int main() {
cin >> T;
//读入数据
for (int i = 1; i <= T; i++) {
cin >> arr[i];
}
cout << list(T) << endl;
return 0;
}