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 ...
随机推荐
- C++面试常见问题——16函数模板的使用
函数模板的使用 函数模板在使用之前必须在外部对函数模板进行初始化. 函数模板的实例化包含两中 1.隐式实例化: template <class T> //没有: T Fun(T a,T b ...
- 面试题之xml解析?
题目是:用java程序将xml中的数据保存到实体对象中,如何实现? xml如下: <?xml version="1.0" encoding="UTF-8" ...
- Day8 - G - Bound Found ZOJ - 1964
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- Java之反射 — 用法及原理
Java之反射 - 用法及原理 定义 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象 ...
- 04.Delphi通过接口IInterface实现多重继承
IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 uSayHello代码如下 unit uSayHello; interface uses SysUtils, ...
- Spark Scheduler 模块(下)
Scheduler 模块中最重要的两个类是 DAGScheduler 和 TaskScheduler.上篇讲了 DAGScheduler,这篇讲 TaskScheduler. TaskSchedule ...
- linux下nginx的安装和配置
准备目录 [root@sqh ~]# mkdir /usr/local/nginx [root@sqh ~]# cd /usr/local/nginx 添加一些支持和依赖 1.安装gcc 安装redi ...
- 中文文本分类之CharCNN
文本分类是自然语言处理中一个非常经典的任务,可用的模型非常多,相关的开源代码也非常多了.这篇博客用一个CNN模型,对新闻文本进行分类. 全部代码有4个模块:1.数据处理模块(命名为:cnews_loa ...
- python itertools 用法
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存.使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环.- 无限迭代器 itertools包 ...
- 132-PHP子类和父类同名函数的调用
<?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...