描述


http://poj.org/problem?id=2385

两棵苹果树,给定一个时间t,1~t每分钟有一棵树掉苹果,牛起始在#1树,最多换w次位置,问最多接到多少苹果.

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10522   Accepted: 5106

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

Each minute, one of the two apple trees drops an apple. Bessie,
having much practice, can catch an apple if she is standing under a tree
from which one falls. While Bessie can walk between the two trees
quickly (in much less than a minute), she can stand under only one tree
at any time. Moreover, cows do not get a lot of exercise, so she is not
willing to walk back and forth between the trees endlessly (and thus
misses some apples).

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes.
Bessie is willing to walk back and forth at most W (1 <= W <= 30)
times. Given which tree will drop an apple each minute, determine the
maximum number of apples which Bessie can catch. Bessie starts at tree
1.

Input

* Line 1: Two space separated integers: T and W

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS:

Seven apples fall - one from tree 2, then two in a row from tree 1,
then two in a row from tree 2, then two in a row from tree 1. Bessie is
willing to walk from one tree to the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the first
two have dropped, then moving to tree 2 for the next two, then returning
back to tree 1 for the final two.

Source

分析


用f[i][j][k]表示第i分钟,已经移动了j次,在#k树下的最优解.

注意:

1.有些时候动规写成+的形式比-的形式方便

 #include<cstdio>
#include<algorithm>
using std :: max; const int maxt=,maxw=;
int t,w;
int tree[maxt],f[maxt][maxw][]; inline int move(int x) { return x== ? : ; } void solve()
{
int ans=;
for(int i=;i<t;i++)
{
for(int j=;j<=w;j++)
{
for(int k=;k<=;k++)
{
if(k==tree[i+])
{
f[i+][j][k]=max(f[i+][j][k],f[i][j][k]+);
f[i+][j+][move(k)]=max(f[i+][j+][move(k)],f[i][j][k]);
}
else
{
f[i+][j][k]=max(f[i+][j][k],f[i][j][k]);
f[i+][j+][move(k)]=max(f[i+][j+][move(k)],f[i][j][k]+);
}
}
}
}
for(int i=;i<=t;i++)
{
for(int j=;j<=w;j++)
{
for(int k=;k<=;k++)
{
ans=max(ans,f[i][j][k]);
}
}
}
printf("%d\n",ans);
} void init()
{
scanf("%d%d",&t,&w);
for(int i=;i<=t;i++)
{
scanf("%d",tree+i);
}
if(tree[]==) f[][][]=;
else f[][][]=;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("apple.in","r",stdin);
freopen("apple.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}

POJ_2385_Apple_Catching_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. MVC小系列(一)【制作表格】

    在Razor引擎中,对于在表格中进行遍历时,一般会这样写 复制代码 <table border="> @{ ; i < ; i++) { <tr> <td ...

  2. Visual C++ 打印编程技术-编程基础

    背景: windows产生前,操作系统(如DOS等)都不提供支持图像处理的打印机驱动程序,使得程序员为打印出图像,不得不针对使用的打印机 自己编写设备驱动程序,导致了大量的.不必要的重复开发. 随着w ...

  3. web services 接口测试方法

    public class Test { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new Jax ...

  4. ECMAScript 6 中的一些新特性

    1.箭头函数,直接写出来v =>看不出来什么,但是跟传统写法一比较,很直观地就能看出v =>是代替了匿名函数 function(v)的写法,{}与逻辑照旧,但是要注意,=与>之间不能 ...

  5. <s:iterator></s:iterator>循环指定输出,(status的方法使用)

    list集合中的实体的一个属性是另一个实体的集合(如下) public class PetInfo { private int petId; private String private Set< ...

  6. c语言"a<b<c"条件值的判定

    示例代码: #include <stdio.h> int main() { , b = , c = ; ; while (a<b<c) { t = a; a = b; b = ...

  7. webapp中的日期选择

    你是否在开发webapp时,选择用哪种第三方日期选择控件绞尽脑汁? 其实不用那么麻烦,现在移动端都是WebKit内核,支持HTML5,其实只要弱弱的将input中将type="date&qu ...

  8. jQuery移除指定元素后的所有元素

    jQuery 遍历的nextAll() 方法可以搜索 DOM 树中的元素跟随的同胞元素,也就是一个元素后面的所有同级元素,删除可以使用方法remove(),所以连起来为 $(selector).nex ...

  9. php计算最后一次,第一次字符串出现位置

    strpos($str, n) 首次,n在str第一次出现位置, strrpos($str, n) 最后一次,n在str最后一次出现位置 strripos区分大小写

  10. Activity的窗口对象(Window)的创建过程分析

    与Activity组件所关联的窗口对象的实际类型为PhoneWindow,后者是从Window类继承下来的. Activity.Window和PhoneWindow三个类的关系如下 PhoneWind ...