Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

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.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1010][1010][2];
int num[1010];
int main()
{
int n,time;
int maxx=-1000;
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&time);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
{
dp[i][0][0]=dp[i-1][0][0]+(num[i]==1);
dp[i][0][1]=dp[i-1][0][1]+(num[i]==2);
for(int j=1;j<=time;j++)
{
dp[i][j][0]=max(dp[i-1][j-1][1],dp[i-1][j][0])+(num[i]==1);
dp[i][j][1]=max(dp[i-1][j-1][0],dp[i-1][j][1])+(num[i]==2);
maxx=max(maxx,max(dp[i][j][0],dp[i][j][1]));
}
}
printf("%d\n",maxx);
return 0;
}

poj--2385--Apple Catching(状态dp)的更多相关文章

  1. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...

  2. POJ 2385 Apple Catching ( 经典DP )

    题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...

  3. POJ 2385 Apple Catching【DP】

    题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...

  4. POJ - 2385 Apple Catching (dp)

    题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...

  5. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

  6. poj 2385 Apple Catching(dp)

    Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...

  7. poj 2385 Apple Catching(记录结果再利用的动态规划)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...

  8. POJ 2385 Apple Catching

    比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...

  9. POJ 2385 Apple Catching(01背包)

    01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...

  10. 动态规划:POJ No 2385 Apple Catching

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

随机推荐

  1. 【Android】Android程序自己主动更新

    App自己主动更新的步骤可分为三步: 检查更新(假设有更新进行第2步,否则返回) 下载新版的APK安装包 安装APK 以下对这三步进行解释.当中会穿插相应代码.App自己主动更新的这三步所有被封装到了 ...

  2. hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java

      import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...

  4. BZOJ 1898 构造矩阵+矩阵快速幂

    思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...

  5. Linux换行符相关

    Linux和windows中的换行符差异问题LINUX的换行符在Windows记事本打开不换行或出现黑点是由于Linux和windows中的换行符差异问题造成的. Unix系统里,每行结尾只有“< ...

  6. 关闭WPS屏保

    WPS作为国产的Office,可谓是越来越流氓,无缘无故给你弄个屏保,不好好做Office就知道如何圈钱,或许这就是国内IT行业和国外的差距吧. 我用的WPS内部版本是:10.1.0.7311 如何查 ...

  7. (转载)ScratchView

    首页 我的管理 客户端 登录注册 首页 安卓组件中心 安卓代码分享 Swift专区 HTML5移动开发 视频中心 小程序 名称: ScratchView 作者: doliangzhe 来源: gith ...

  8. Android官方培训课程中文版(v0.9.7)

    Android官方培训课程中文版(v0.9.7) Google Android团队在2012年的时候开设了Android Training板块 - http://developer.android.c ...

  9. (转载)android 一些工具类汇总

    android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...

  10. 利用SQL索引提高查询速度

    1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引结构. 索引的使用要恰到好处,其使用原则如下: 在经常进行连接 ...