POJ 2385 Apple Catching(DP)
来源:互联网 发布:java线程 编辑:程序博客网 时间:2024/05/16 09:56
http://poj.org/problem?id=2385
题意:现在有两棵苹果树,编号为1、2,在第i秒的时候其中一棵树会落下苹果。人可以瞬时移动到另外一棵树那里。现在有n秒,人最多移动m次,最多可以接到多少苹果。人一开始在第一棵苹果树下面。
如果用秒定义,那么会说不清,还是用苹果吧。dp[i][j]表示在前i个苹果落下后移动j次可以接到最多的苹果数,j给出,人的位置是确定的。分为两种情况讨论,第一种在第i-1个苹果落下了但第i个苹果未落下时不移动,第二种移动。那么根据现在人所在的位置和苹果即将掉下的位置,可以判断人能不能接到这个苹果。两种情况对应的之前接到的苹果数分别是d[i-1][j]和d[i-1][j-1]。由于是i-1,j-1,所以dp[0][j]dp[i][0]要初始化。dp[0][j]一定都是0,dp[i][0]就是人一直站在第一棵树下面在不同秒数可以接到的苹果数。
#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespace std;int n, m, tmp, a[1005], dp[1005][50], p, ans;int main(){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(dp, 0, sizeof(dp)); tmp = 0; for (int i = 1; i <= n; i++) { if (a[i] == 1) tmp++; dp[i][0] = tmp; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if ((j+1) % 2 == 1) { if (a[i] == 1) p = 1; else p = 0; } else { if (a[i] == 2) p = 1; else p = 0; } dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + p; } for (int i = 0; i <= m; i++) ans = max(ans, dp[n][i]); printf("%d\n", ans); return 0;}
0 0
- POJ 2385 Apple Catching (DP)
- POJ 2385 Apple Catching (dp)
- Apple Catching (poj 2385 简单dp)
- poj--2385--Apple Catching(状态dp)
- POJ 2385 Apple Catching(DP)
- POJ 2385 Apple Catching(基础DP)
- POJ - 2385 Apple Catching (DP)
- POJ 2385 Apple Catching(线性DP)
- POJ 2385 Apple Catching(DP)
- POJ 2385 Apple Catching DP
- POJ 2385 Apple Catching [DP]
- POJ 2385 Apple Catching DP
- poj 2385 Apple Catching DP
- POJ 2385 Apple Catching (DP)
- POJ 2385 Apple Catching DP
- poj 2385 Apple Catching DP
- poj 2385 Apple Catching dp
- poj 2385 Apple Catching DP
- ORACLE新建用户及建表
- Codeforce 630B Moore's Law
- 复杂指针的声明和解读
- FreeBSD与Linux的比较
- Java中的static
- POJ 2385 Apple Catching(DP)
- 子类的继承性
- PHP中常用的设计模式
- uva816
- Linux 线程锁
- 关于Android Studio升级到2.0后和Gradle插件不兼容的问题
- 第二章 IPC机制
- Codeforces 630 B. Moore's Law
- bash: sqlplus: command not found 解决方法