题目传送门

Apple Catching

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15444   Accepted: 7582

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

题意:有两棵树,给你t分钟,每分钟有一棵树会掉落一颗苹果,再给你w个体力,每次从一棵树移动到另一棵树需要消耗1个体力,一开始你再第一棵树那里,问你t分钟后你最多能得到几个苹果?

题解:定义dp[i][j]:前i分钟后移动j步得到的最多的苹果数

那么这到第i分钟的状态就取决于前i-1分钟移动或者不移动得到的苹果树

即dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//注意边界

还有怎么判断移动j步到哪棵树了呢,很简单,

就是偶数步回到起点,奇数步到第二棵树

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define MAX 1005
int dp[MAX][];
int a[MAX];
int main()
{
int n,w;
while(~scanf("%d %d",&n,&w)){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=w;j++)
{
if(j>=) dp[i][j]=max(dp[i-][j],dp[i-][j-]);
else dp[i][j]=dp[i-][j];
if(j%+==a[i]) dp[i][j]++;
}
}
int ans=;
for(int i=;i<=w;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
} return ;
}

Apple Catching

最新文章

  1. context:component-scan&quot; 的前缀 &quot;context&quot; 未绑定。
  2. 读《编写可维护的JavaScript》第八章总结
  3. footer元素
  4. Atitit.json&#160;xml&#160;序列化循环引用解决方案json
  5. [整理]Selector、shape详解
  6. DB天气app冲刺第二天
  7. smarty 自定义函数
  8. MEF初体验之八:过滤目录
  9. android开源应用(主要是博客上带有分析的)收集 【持续更新】
  10. hdu_1115_Lifting the Stone(求多边形重心)
  11. zf-关于通知公告如果发布的是无限制时间的,那么默认隐藏时间输入框的问题
  12. UltraEdit的配置
  13. R语言并行计算中的内存控制
  14. SpringMVC中的文件上传
  15. 关于select的id以及value传给后台的问题
  16. pyCharm-激活码(2018)
  17. C# 树状图
  18. 内联元素于与块元素的转换 相对定位、绝对定位以及fixed定位 Z轴覆盖
  19. 快速开发android,离不开这10个优秀的开源项目
  20. Nginx URL后面不加斜杠301重定向

热门文章

  1. Centos 7 环境下安装 RabbitMQ 3.6.10
  2. ghci对haskell的类型推导
  3. 线程工具类 - CountDownLatch(倒计时器)
  4. 转载-使用Nodepad++来编辑我们服务器的配置文件
  5. 最佳实践 | 数据库迁云解决方案选型 &amp; 流程全解析
  6. 学习日记9、easyui控件两次请求服务器端问题
  7. Facebook发布Tweaks:让微调iOS应用变得更简单
  8. 使用 localstorage 写入浏览器并获取
  9. 【进阶技术】一篇文章搞掂:Spring Cloud Stream
  10. Spring源码解读--(一)源码下载