POJ 2385 Apple Catching【DP】
题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果。
思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只能由两个状态转移而来。
即上一时刻同一棵树或上一时刻不同的树
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]);
则这一时刻在转移次数为j的情况下最多能接到的苹果为那两个状态的最大值再加上当前能接受到的苹果。(注意当前能否拿到苹果只与转移次数有关)
if (j % 2 + 1 == apple[i])dp[i][j]++;
#include <iostream>
#include <cstdio>
#include <queue>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std; int dp[1001][31];
int apple[1001];
int main()
{
int t,w;
scanf("%d%d", &t,&w);
memset(dp, 0, sizeof(dp));
for(int i=1;i<=t;i++)
scanf("%d",apple+i);
for(int i=1;i<=t;i++)
for(int j=0;j<=w;j++)
{
if(j == 0)//注意
dp[i][j] = dp[i-1][j];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
if(j%2+1==apple[i])dp[i][j]++;
}
int ans=0;
for(int i=1;i<=t;i++)
for(int j=0;j<=w;j++)
ans=max(ans,dp[i][j]);
printf("%d",ans);
return 0;
}
POJ 2385 Apple Catching【DP】的更多相关文章
- 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 ( 经典DP )
题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...
- POJ - 2385 Apple Catching (dp)
题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- POJ 3280 Cheapest Palindrome【DP】
题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...
- poj 2385 Apple Catching(dp)
Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...
- poj 2385 Apple Catching(记录结果再利用的动态规划)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...
- POJ 1661 Help Jimmy【DP】
基础DP,过程想明白了其实也不复杂,从上面的推下面的比倒着推要简单很多.调试了半个多小时..简单dp依然不能快速AC..SAD.. 题目链接: http://poj.org/problem?id=16 ...
- poj2385 - Apple Catching【动态规划】
Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...
随机推荐
- Dubbo是什么
读音 |ˈdʌbəʊ| !不是[dubəʊ]! (重点:调用步骤) Dubbo是什么 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.其核心 ...
- Java中的回车换行符/n /r /t
'\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格.通常用的Enter是两个加起来.下面转一篇文章. 回车和换行 今天,我总算搞清楚“回车”(carriage return)和“换行 ...
- Gym - 100085G - GCD Guessing Game
原题链接 题意一个数字x在1-n之间,现在猜数字,每次猜一个数字a,告知gcd(x, a)的答案,问最坏情况下需要猜几次 分析 考虑素数.当猜的数为一组素数的乘积时,就可以把这些素数都猜出来.那么答案 ...
- Spring RedisTemplate操作-序列化性能测试(12)
@Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...
- android ListView使用
1.DbOpenHelper package dbOpenHelper; import android.content.Context; import android.database.sqlite. ...
- android彻底关闭应用程序方法
Android SDK > 7(Android2.1)之后,即Android2.2及以后版本彻底关闭应用的方法,目前试验只有一下方法有效: Intent startMain = new Inte ...
- Android五种数据存储方式
android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...
- ActiveMQ集成Spring使用
现在任何一个框架的使用都会结合spring框架,quartz.cxf与平时常见的Hibernate.mybatis.Struts等都可以与spring集成起来使用,在这里研究了activemq结合sp ...
- Linux分区设置
基本3个就可以了 序号 路径 大小 格式 ① /boot 200MB ext4 ② 物理内存*1-1.5 swap ③ / 剩余存储空间 ext4
- AD域相关的属性和C#操作AD域
“常规”标签 姓 Sn 名 Givename 英文缩写 Initials 显示名称 displayName 描述 Description 办公室 physicalDeliveryOfficeNam ...