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

本文介绍了一种求解递减最长子序列问题的算法实现,通过动态规划的方法找到序列中递减顺序最长的子序列长度。代码示例使用C++实现,并详细展示了输入处理及动态转移方程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值