POJ_2385_Apple_Catching_(动态规划)
描述
http://poj.org/problem?id=2385
两棵苹果树,给定一个时间t,1~t每分钟有一棵树掉苹果,牛起始在#1树,最多换w次位置,问最多接到多少苹果.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10522 | Accepted: 5106 |
Description
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
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
Sample Input
7 2
2
1
1
2
2
1
1
Sample Output
6
Hint
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_(动态规划)的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- Python入门 学习笔记 (一)
原文地址:http://www.cnblogs.com/lujianwenance/p/5939786.html 说到学习一门语言第一步就是先选定使用的工具(环境).因为本人是个小白,所以征求了一下同 ...
- javascript DOM操作 第19节
<html> <head> <title>DOM对象</title> <script type="text/javascript&quo ...
- Ambiguous handler methods mapped for HTTP path
一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...
- Cocos2d-x 3.0坐标系详解(转载)
Cocos2d-x 3.0坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenG ...
- Demo_张仕传_结构体考试-modify
/* 题目: //声明一个结构体类型 struct _AdvTeacher { char *name; char *tile; int age; char *addr; char *p1; //系统预 ...
- Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)
Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...
- Iptables網路連線限制及攻擊防護和相關設定
[筆記整理]Iptables網路連線限制及攻擊防護和相關設定 1. 限制每個IP連接HTTP最大併發50個連接數 iptables -A INPUT -p tcp --dport 80 -m conn ...
- 在hyper安装openwrt
写了长长长一篇文章,结果把标签关了,这篇文章就不见了,草稿箱也没有!!! 只好直接copy原来作者的文章了 下载地址 openwrt image tulip driver 引用 Want to add ...
- java中运算符——进度1
Class Demo1{ public static void main(String[] args) { /* 一.逻辑运算法用于连接两个boolean类型的表达式 ...
- [转] 使用maven运行java main的三种方式
原文地址: http://blog.csdn.net/qbg19881206/article/details/19850857?utm_source=tuicool&utm_medium=re ...