题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果。假设该人一开始在标号为1的树下。

分析:

1、dp[x][y][z]---第x分钟移动了y次的情况下,现在位于标号为z的树下最多吃到的苹果数。

2、枚举所有的时间、次数和可能位于的树下。

若第i分钟该人位于标号为k的树下,第i + 1分钟苹果从标号为a[i+1]的树上落下。

(1)k==a[i],

此人站在标号为k的树下不移动,即可再吃到一个苹果:dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);

此人多方面权衡后选择移动到另一棵树下,则移动但吃不到苹果:dp[i + 1][j + 1][Move(k)] = max(dp[i + 1][j + 1][Move(k)], dp[i][j][k]);

(2)k!=a[i],

此人移动到另一棵树下,即可再吃到一个苹果:dp[i + 1][j + 1][Move(k)] = max(dp[i + 1][j + 1][Move(k)], dp[i][j][k] + 1);

此人多方面权衡后选择不移动,因此吃不到苹果:dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXN][35][3];
int a[MAXN];
int Move(int k){
return k == 1 ? 2 : 1;
}
int main(){
int T, W;
scanf("%d%d", &T, &W);
for(int i = 1; i <= T; ++i){
scanf("%d", &a[i]);
}
if(a[1] == 1){
dp[1][0][1] = 1;
}
else{
dp[1][1][2] = 1;
}
for(int i = 1; i <= T - 1; ++i){
for(int j = 0; j <= W; ++j){
for(int k = 1; k <= 2; ++k){
if(k == a[i + 1]){
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
dp[i + 1][j + 1][Move(k)] = max(dp[i + 1][j + 1][Move(k)], dp[i][j][k]);
}
else{
dp[i + 1][j + 1][Move(k)] = max(dp[i + 1][j + 1][Move(k)], dp[i][j][k] + 1);
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
}
}
}
}
int ans = 0;
for(int i = 0; i <= W; ++i){
for(int j = 1; j <= 2; ++j){
ans = max(ans, dp[T][i][j]);
}
}
printf("%d\n", ans);
return 0;
}

  

POJ - 2385 Apple Catching (dp)的更多相关文章

  1. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

  2. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...

  3. POJ 2385 Apple Catching(01背包)

    01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...

  4. POJ 2385 Apple Catching【DP】

    题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...

  5. POJ 2385 Apple Catching ( 经典DP )

    题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. 【POJ - 2385】Apple Catching(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  8. POJ:2385-Apple Catching(dp经典题)

    Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14311 Accepted: 7000 Descr ...

  9. POJ 3858 Hurry Plotter(DP)

    Description A plotter is a vector graphics printing device that connects to a computer to print grap ...

随机推荐

  1. 034、Java中自增之++在前面的写法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. RPC_E_SERVERFAULT excel com操作错误

    遇到多次了,以管理员身份启动excel,禁用第三方加载项,解决. 我遇到几次都是 foxit pdf reader导致的.

  3. mysql 视图入门

  4. lpwizard 生成的 allegro 封装中 .psx 文件使用方法。

    lpwizard 有时候生成 allegro 封装的时候会生成 .psx 文件,这个文件其实是脚本文件,用于某些特殊形状焊盘的处理. 具体的使用方法如下: 在Allegro中,选择 File > ...

  5. 吴裕雄--天生自然java开发常用类库学习笔记:Set接口

    import java.util.HashSet ; import java.util.Set ; public class HashSetDemo01{ public static void mai ...

  6. Golang的运算符-位运算符

    Golang的运算符-位运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位运算符概述 常见的位逻辑运算符: &: 位与运算符,表示AND(表示所有条件都得匹配), ...

  7. CodeForces - 755C PolandBall and Forest (并查集)

    题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...

  8. js 跳转链接

    1.跳转链接 在当前窗口打开 window.location.href="http://www.baidu.com" 等价于 <a href="baidu.com& ...

  9. htmp to pdf

    C++ Library to Convert HTML to PDF html2pdf PrinceXML 收费 CutePDF Ghostscript PDFDoc VisPDF PDFDoc Sc ...

  10. 通过fiddler修改通讯返回值

      1 在fiddler里选中url,右键unlock for editing 2 在fiddler里点击url, 在右面的返回值的 TextView 项里修改数据 3 取消 unlock for e ...