POJ-2533.Longest Ordered Subsequence (LIS模版题)
本题大意:和LIS一样
本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i)
参考代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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[maxn]; 8 9 int main () { 10 int n, ans = 0; 11 cin >> n; 12 for(int i = 1; i <= n; i ++) 13 cin >> a[i]; 14 for(int i = 1; i <= n; i ++) { 15 dp[i] = 1; 16 for(int j = 1; j < i; j ++) { 17 if(a[i] > a[j]) dp[i] = max(dp[i], dp[j] + 1); 18 } 19 ans = dp[i] > ans ? dp[i] : ans; 20 } 21 cout << ans << endl; 22 return 0; 23 }
时间并不会因为你的迷茫和迟疑而停留,就在你看这篇文章的同时,不知道有多少人在冥思苦想,在为算法废寝忘食,不知道有多少人在狂热地拍着代码,不知道又有多少提交一遍又一遍地刷新着OJ的status页面……
没有谁生来就是神牛,而千里之行,始于足下!