The longest common non-descending subsequence (LIS)
Problem Description
In a numeric sequence, find the longest sub-sequence (may not be continuous), so that this sub-sequence is not descending (non-decreasing).
Sample input
7
1 2 3 -1 -2 7 9
Sample output
5
Dynamic transfer equation: dp[i] = max{1, dp[j] + 1}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int s[n];
for(int i = 0; i < n; i++){
cin>>s[i];
}
int dp[n];
memset(dp, 0, sizeof(dp));
int ans = -1; //记录结果
for(int i = 0; i < n; i++){
dp[i] = 1;
for(int j = 0; j < i; j++){
if(s[i] >= s[j] && dp[j] + 1 > dp[i]){
dp[i] = dp[j] + 1; //动态转移方程
}
}
ans = max(ans, dp[i]);
}
cout<<ans;
return 0;
}