Apple Catching

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 14311 Accepted: 7000

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.


解题心得:

  1. 题意就是有两颗苹果树,每一秒钟某一颗树上会掉下来一颗苹果,一个人可以在两棵树下移动,移动时间不计,最多可以移动k次,问这个人最多可以获得多少颗苹果。
  2. 其实这个题最直观的做法就是用记忆化搜索,直接按照题意搜索就行了。当然也可以将记忆化搜索提炼出dp公式出来,写dp状态转移方程式。
  3. 提炼出来dp方程式可以这样表示,dp[i][j]为在i秒最多移动j次可以获得的最大苹果树。状态转移方程为dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + (j%2+1 == arr[i]),因为一开始在1位置,所以在移动j次之后所在的位置为j%2+1,代表在第i秒移动j次的状态只能够从第i-1秒移动j-1次和i-1秒移动j次转移而来,然后加上当前是否可以接到苹果的。
记忆化搜索代码:

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1010;
int dp[maxn][35][3],w,n,scor[maxn]; void init() {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&n,&w);
for(int i=1;i<=n;i++) {
scanf("%d",&scor[i]);
scor[i] %= 2;
}
} int dfs(int times,int change,int pos) {
if(times > n || change > w)
return 0;
if(dp[times][change][pos] != -1)
return dp[times][change][pos];
return dp[times][change][pos] = ((scor[times] == pos) + max(dfs(times+1,change,pos),dfs(times+1,change+1,!pos)));
} int main() {
init();
int ans = max(dfs(1,0,1),dfs(1,0,0));
printf("%d\n",ans);
return 0;
}

dp方程式转移代码

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int dp[maxn][35];
int arr[maxn]; int main() {
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
for(int i=1;i<=n;i++) {
for(int j=0;j<=k;j++) {
if(j == 0) {
dp[i][j] = dp[i-1][j] + (j%2 +1 == arr[i]);
continue;
}
dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]);
if(j%2 + 1 == arr[i])
dp[i][j]++;
}
} int ans = 0;
for(int i=0;i<=k;i++)
ans = max(ans,dp[n][i]); printf("%d\n",ans);
return 0;
}

最新文章

  1. 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】
  2. (python)对象的引用
  3. angularJS自定义那些事
  4. Shell命令_awk命令
  5. Lambert漫反射.BLinnPhong及Phong模型 Unity自带的在Lighting.cginc里
  6. Turing Tree_线段树&amp;树状数组
  7. 将服务器上的myql数据库导入本地数据库
  8. 【linux设备模型】之platform设备驱动
  9. C# 之 遍历本地文件夹下的所有文件
  10. [状压dp]POJ2686 Traveling by Stagecoach
  11. 批处理,修改环境变量path的方法(加环境变量)
  12. (转)SQL流程控制语句学习(二):begin…end if…else case
  13. ABP .Net Core Entity Framework迁移使用MySql数据库
  14. 【Linux】 环境变量与shell配置&amp;执行
  15. mysql杯观锁与乐观锁
  16. 函数QFileSystemModelPrivate::_q_fileSystemChanged
  17. npm 命令
  18. Python 文件编译为字节码的方法
  19. Ubuntu常用软件安装(小集合)
  20. mysql数据类型介绍(含text,longtext,mediumtext说明)

热门文章

  1. tween.js 插件
  2. POJO详解
  3. 第二章 LCD液晶显示屏&amp;声控装置&amp;播放音乐&amp;遥控器
  4. April 26 2017 Week 17 Wednesday
  5. python IDE-pycharm在virtualenv里安装软件
  6. andriod给ListView中的TextView增加跑马灯效果
  7. React的安装
  8. tensorflow与android编译
  9. ios各层
  10. 大白话讲解BP算法(转载)