POJ-1887-Testing the CATCHER-最长递减子序列-DP动态规划

跟Longest Ordered Subsequence类似,只不过现在是递减的。再有就是要注意下输入如何进行处理。

动态转移方程为:

dp[i] = max(1, dp[j] + 1), 0 <= j <= i - 1, 且a[j] > a[i]

代码

#include <algorithm> #include <iostream> using namespace std; const int MAX_SIZE = 5005; int a[MAX_SIZE]; int dp[MAX_SIZE]; int main() { int test = 1; int temp; while(1) { cin >> temp; if(temp == -1) break; int n = 0; a[0] = temp; n++; while(1) { cin >> temp; if(temp == -1) break; a[n] = temp; n++; } for(int i = 0; i < n; i++) { dp[i] = 1; for(int j = 0; j < i; j++) { if(a[j] > a[i] && dp[i] < dp[j] + 1) dp[i] = dp[j] + 1; } } int max_dp = *max_element(dp, dp + n); cout << "Test #" << test << ":" << endl; cout << " maximum possible interceptions: " << max_dp << endl; cout << endl; test++; } return 0; }

你可能感兴趣的:(POJ-1887-Testing the CATCHER-最长递减子序列-DP动态规划)