POJ - 2385 Apple Catching (dp)
题意:有两棵树,标号为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)的更多相关文章
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- poj 2385 Apple Catching 基础dp
Apple Catching Description It is a little known fact that cows love apples. Farmer John has two ap ...
- POJ 2385 Apple Catching(01背包)
01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...
- POJ 2385 Apple Catching【DP】
题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...
- POJ 2385 Apple Catching ( 经典DP )
题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- POJ:2385-Apple Catching(dp经典题)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14311 Accepted: 7000 Descr ...
- POJ 3858 Hurry Plotter(DP)
Description A plotter is a vector graphics printing device that connects to a computer to print grap ...
随机推荐
- 并行执行 Job【转】
有时,我们希望能同时运行多个 Pod,提高 Job 的执行效率.这个可以通过 parallelism 设置. 这里我们将并行的 Pod 数量设置为 2,实践一下: Job 一共启动了两个 Pod,而且 ...
- k8s node断电重启
kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...
- Spark Deploy 模块
Spark Scheduler 模块的文章中,介绍到 Spark 将底层的资源管理和上层的任务调度分离开来,一般而言,底层的资源管理会使用第三方的平台,如 YARN 和 Mesos.为了方便用户测试和 ...
- HDU - 6205 card card card (尺取法)
题意:有n堆牌,ai表示每堆牌的牌数,bi表示每堆牌的penaltyvalue,操作开始前,可以重复进行将第一堆牌挪到最后一堆这一操作.然后,对于挪完后的牌,从第一堆开始,依次取.对于每一堆牌,首先将 ...
- 那些年我们一起踩过的坑(javascript常见的陷阱)
1.object最后一个逗号 定义object直接量或json,最后一个逗号多写了,在ie下会报错,高级浏览器则不会,给只使用chrome调试的同学敲个警钟.踩了无数次这个坑了. 2.自动加分号 ...
- tomcat和servlet容器的关系
- 吴裕雄--天生自然C++语言学习笔记:C++ 运算符
运算符是一种告诉编译器执行特定的数学或逻辑操作的符号.C++ 内置了丰富的运算符,并提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 杂项运算符 算术运算符 下表显示 ...
- junit基础学习之-简介(1)
JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...
- Python 内置类型 dict, list,线程安全吗
近段时间发现一个 Python 连接数据库的连接是线程不安全的,结果惹得我哪哪儿都怀疑变量的多线程是否安全的问题,今天终于找到了正确答案,那就是 Python 内置类型 dict,list ,tupl ...
- Centos 7 x86_64 环境Python2.7升级Python3.7.4
升级Python3.7.4 #安装补丁包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel read ...