题意:有两棵树,标号为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. impala invalidate metadata和impala-shell -r作用相同

    impala的invalidate metadata内部命令,是否和外部命令impala-shell -r的作用相同的? 这个问题的回答: 在invalidate metadata 和 impala- ...

  2. vSphere中Storage vMotion的流程详解

    内容预览: 1. Storage vMotion的迁移方式 2. 影响Storage vMotion效率的因素 3. Storage vMotion的详细流程 企业部署虚拟化后,如果发现存储的性能出现 ...

  3. C++面试常见问题——11重载、覆盖、隐藏

    重载.覆盖.隐藏 重载 在同一类定义的成员函数中,参数不同的同名函数为重载关系.重载与虚函数无关. class A{ private: int x; public: void fun(int); // ...

  4. 【pwnable.tw】 starbound

    此题的代码量很大,看了一整天的逻辑代码,没发现什么问题... 整个函数的逻辑主要是红框中两个指针的循环赋值和调用,其中第一个指针是主功能函数,第二个数组是子功能函数. 函数的漏洞主要在main函数中, ...

  5. 【pwnable.kr】 [simple login]

    Download : http://pwnable.kr/bin/login Running at : nc pwnable.kr 9003 先看看ida里面的逻辑. 比较重要的信息时input变量再 ...

  6. mysql提示 Lock wait timeout exceeded解决办法 事务锁死

    查询  select concat('KILL ',id,';') from information_schema.processlist; 复制结果 新建sql脚本粘贴并执行

  7. CVE-2019-0708—微软RDP远程桌面代码执行漏洞复现

    0x01 2019年9月7日凌晨,msf上更新了0708的漏洞利用程序. 顿时安全群和朋友圈就爆炸了 - 奈何接到HW攻击队任务,又在家过了个中秋,0708才在今天更新. 0x02 环境 Window ...

  8. 使用线程池测试cpu的并发计算能力

    接到一个需求是测试一下cpu并发计算能力,针对int和float求和单位时间能执行几次的问题.可能是服务器选型用到的参数. 开始使用的是fork-join,但是发现fork-join每次得到的结果值波 ...

  9. 007.CI4框架CodeIgniter, 加载自己的helper辅助类,调用自己helper中定义各种全局函数

    01. 我们在Helpers文件中创建一个Tx_helper.php的文件,里面就下一个函数 <?php //输出 function ShowMessage($AMsg) { echo &quo ...

  10. 0108 spring的申明式事务

    背景 互联网的金融和电商行业,最关注数据库事务. 业务核心 说明 金融行业-金融产品金额 不允许发生错误 电商行业-商品交易金额,商品库存 不允许发生错误 面临的难点: 高并发下保证: 数据一致性,高 ...