DP:Apple Catching(POJ 2385)
问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的牛最多可以吃到多少个苹果
首先我们应该很容易想到,这个必须要用DP去做,然后就是考虑怎么储存旧值的问题了,因为树有两棵,然后每个往返状态对应不同的结果,所以我们应该用一个二维矩阵去储存(苹果的个数就不重要了,因为我们只用算到最后,前面的都可以扔掉)。
然后状态方程应该怎么写呢?也很简单,定义一个状态为dp[i][j]表示在第i棵树的剩余多少次转移机会时的能吃到苹果的最大个数,而牛可以从旁边一棵树走过来也可以是本来就在这棵树旁边
所以状态转移方程很自然的就是
dp[i][j]=max(dp[i][j],dp[i-1][j+1])+1 (j<w && i+1>w-j)
dp[i][j]=dp[i][j]+1;(j=w)
同时还要注意有一些状态是没有意义的,典型就是i+1<w-j的时候(你想一下苹果掉落的分钟数都比牛跑过来的次数还要少,怎么可能嘛!)
#include <stdio.h>
#include <stdlib.h>
#define MAX(a,b) (a)>(b)?(a):(b) static int Tree[][]; int main(void)
{
int T, W, i, k, Which_Tree, ans = ;
while (~scanf("%d%d", &T, &W))
{
for (i = ; i < T; i++)
{
scanf("%d", &Which_Tree);
Tree[Which_Tree - ][W]++;
for (k = W - ; k >= && i + > W - k; k--)
Tree[Which_Tree - ][k] =
MAX(Tree[!(Which_Tree - )][k + ] + , Tree[Which_Tree - ][k] + );
}
for (i = ; i < ; i++)
for (k = ; k <= W; k++)
ans = MAX(Tree[i][k], ans);
printf("%d", ans);
} return ;
}
DP:Apple Catching(POJ 2385)的更多相关文章
- Day9 - A - Apple Catching POJ - 2385
Description 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可 ...
- A-Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8759 Accepted: 4264 De ...
- 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)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )
dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
随机推荐
- maven加载spring包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artif ...
- UVa 12505 Searching in sqrt(n)
传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- 史上最详细的Android Studio系列教程四--Gradle基础
https://segmentfault.com/a/1190000002439306#articleHeader0
- automapper初步
首先引入 automapper.dll using System; using System.Collections.Generic; using System.Linq; using System. ...
- SCI完全攻略:从构思到发表
- 我和NLP的故事(转载)
正值ACL录用结果发布,国内的老师和同学们又是一次大丰收,在这里再次恭喜所有论文被录用的老师和同学们!我人品爆发,也收获了自己硕士阶段的第二篇ACL论文.本来只是想单纯分享下自己中论文的喜悦,但没成想 ...
- vim tab 和4个空格
在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...
- Js日期选择器并自动加入到输入框中
<html> <head> <title>Js日期选择器并自动加入到输入框中</title> <meta http-equiv="con ...
- linux 访问tomcat 管理页面时 You are not authorized to view this page 403(真实可用)
ava代码 收藏代码 You are not authorized to view this page. If you have not changed any configuration files ...