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 ...
随机推荐
- OS、浏览器排名:Win10狂飙、Chrome逆天
根据 Netmarketshare公布的最新数据,2019年7月,Windows 10系统市场份额获得显著增长,市场份额创下新高:Windows 7则进一步衰退,份额下滑高达3.6%,这也是其历史上最 ...
- 51nod 1444:破坏道路 广度优先搜索
1444 破坏道路 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 在某一个国家,那儿有n个城市,他们通过 ...
- zigbee CC2530首选方案模组:TZU06A1
模块特点 微型24-pin 邮票式SMT 封装 提供U.FL 接口,用于外接SMA 天线 小尺寸封装:16mm*20mm*3.7mm 通过欧盟CE0168.欧盟ROHS 认证 基于8051 单片机架构 ...
- spring-boot-autoconfigure-xx.jar核心注解
- Java笔记--集合
1.Java集合类可以用于存储数量不等的多个对象,还可以用于保存具有映射关系的关联数组. 2.Java集合可分为Collection和Map两种体系: --Collection:1)Set:元素无序. ...
- Day4 - M - Roads in Berland CodeForces - 25C
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...
- 关于SSM中mybatis向oracle添加语句采用序列自增的问题
在SSM向oracle数据库中插入语句时,报错如下: ### Error updating database. Cause: java.sql.SQLException: 不支持的特性 ### SQ ...
- websocket与http
偶然在知乎上看到一篇回帖,瞬间觉得之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗 ...
- 指令——df
df是disk free 的简称,这个指令的功能和作用是查看磁盘空间. 可以加上 -h 的选项,来提高可读性. [he@localhost ~]$ df -h文件系统(磁盘名称) 总容量 ...
- (二)Spring初步搭建、IOC创建对象
环境准备: 见java环境搭建 新建maven项目,同时搭好项目结构,新建相应的包 Spring的初步搭建 1.导入jar包,Spring版本为5.1.10,同时导入junit包 <depend ...