题目大意:
http://codevs.cn/problem/1044/
第一问题就是求序列的最长递减数列的长度,第二问就是求数列的最长递增序列的长度。
代码:
#include <iostream> using namespace std; int arr[30] = {0}; int dp[30] = {0}; int mp[30] = {0}; int main() { int n,count = 1,ans1 =0,ans2 = 0; while(cin >> n) { arr[count] = n; count++; } count--; dp[0] = 0; mp[0] = 0; for(int i = 1; i <= count; i++) { for(int j = i; j >= 1; j--) { if(arr[i] <= arr[j]) ans1 = max(ans1,dp[i] = max(dp[j]+1,dp[i])); } } cout << ans1 << endl; for(int i = 1; i <= count; i++) { for(int j = 0; j < i; j++) { if(arr[i] > arr[j]) ans2 = max(ans2,mp[i] = max(mp[i],mp[j]+1)); } } cout << ans2; return 0; }