poj2385 递推
来源:互联网 发布:阿里云有免费空间吗 编辑:程序博客网 时间:2024/05/19 20:37
题目链接:点击打开链接
题意:
有一只牛要吃苹果,这也有两棵苹果树;
它摘不到苹果只有等着苹果自己掉下来;
苹果在每一秒都会掉一个下来;
牛就在树下来回跑动接住苹果并吃掉;
苹果一共会掉 T 秒;
牛最多在两棵树之间来回跑动 W 次;
问牛在不超过 W 次之内能最多吃到多少苹果;
牛最初站在 1 号树下;
理解:
该题一看就知道是 dp 啦;
所以推一下动态转移方程式;
递推式含义:dp[i][j] 表示在 i 秒时间内最多来回 j 次能吃到的最多的苹果个数;
即:dp[i][j] = max(dp[i - 1][j] + (j % 2 != a[i] % 2), dp[i - 1][j - 1] + (j % 2 != a[i] % 2));
其中 dp[i][0] = dp[i - 1][0] + (j % 2 != a[i] % 2);
初始值都为 0;
最终求得是 max(dp[T][0 ~ W]);
代码如下:
#include <cstdio>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <map>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;typedef pair<int, int> PII;const int MIN_INF = 1e-7;const int MAX_INF = (1e9) + 7;#define X first#define Y secondint dp[1010][40];int a[1010];int main() { int t, w; cin >> t >> w; for (int i = 1; i <= t; ++i) { cin >> a[i]; } for (int i = 1; i <= t; ++i) { for (int j = 0; j <= i && j <= w; ++j) { if (j == 0) { dp[i][j] = dp[i - 1][j] + (j % 2 != a[i] % 2); } else { dp[i][j] = max(dp[i - 1][j] + (j % 2 != a[i] % 2), dp[i - 1][j - 1] + (j % 2 != a[i] % 2)); } } } int mx = -1; for (int i = 0; i <= w; ++i) { mx = max(dp[t][i], mx); } cout << mx << endl; return 0;}
0 0
- poj2385(递推)
- poj2385 递推
- POJ2385 Apple Catching(DP/递推)
- poj2385
- poj2385
- poj2385
- poj2385
- poj2385
- poj2385 dp
- poj2385 dp
- poj2385(dp)
- POJ2385【DP】
- dp-poj2385
- poj2385-dp经典
- poj2385 - Apple Catching
- poj2385(DP)
- POJ2385 Apple Catching
- poj2385-dp经典
- 线程的操作方法
- jboss远程连接配置
- 【嵌入式学习日记】2016年7月26日
- Spring:源码解读Spring IOC原理
- 算法:回溯(试探)算法
- poj2385 递推
- noip 2014 总结
- javascript权威指南--比较运算符
- HDU1159 Common Subsequence (LCS)
- Ext.util.Observable用法
- 实现简单工厂(二)
- 旅行 (贪心+枚举)
- MyEclipse + Pydev开发Python时import报错解决方法
- 学术上讲故事的过程