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. angular-代码段

    重复代码 <div ng-app="" ng-init="names=['Jani','Hege','Kai']"> <p>使用 ng- ...

  2. POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛

    http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS   M ...

  3. HDU 1521

    指数型生成函数.做这题时,回去看看组合数学才知道,指数生成函数求的就是多重集合的r排列数. #include <iostream> #include <cstdio> #inc ...

  4. Coding上部署Ghost博客

    Ghost构建于Node.js平台之上.支持0.10.*版本号的Node.js. 在你的本地计算机上执行Ghost事实上非常easy,前提是你已经安装了Node.js. 什么是Node.js? 略过 ...

  5. Linux以下的两种文件锁

    文件锁是一种文件读写机制.在不论什么特定的时间仅仅同意一个进程訪问一个文件. 利用这样的机制可以使读写单个文件的过程变得更安全. 在这篇文章中.我们将探讨Linux中不同类型的文件锁,并通过演示样例程 ...

  6. Woody的Python学习笔记2

    Python多行语句 Python语句中一般以新行作为语句的结束符.但我们能够使用斜杠(\)将一行的语句分为多行显示,例如以下所看到的: total = item_one+\ item_two + \ ...

  7. m-orchastration system

    m-orchastration system 1.bootstrap上面有很多前台的页面代码可以用 2.H-ui里面的案例我可以去看看,这个网站也有后台框架 H-ui案例 - H-ui前端框架官方网站 ...

  8. zzulioj--1609--求和(数学规律)

     1609: 求和 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 209  Solved: 67 SubmitStatusWeb Board De ...

  9. SQL Server查询数据库空间分配情况、数据库备份信息

    查询数据库空间分配情况: use master go create procedure dbo.proc_getdbspaceused as begin set nocount on create t ...

  10. hiho1469 - 简单dp

    题目链接 题目大意: 从一个大正方形数组里面找一个小正方形,满足其中的每个位置上的数都恰好比他的左边的那个和上边的那个大1(如果左边或上边的那个不存在的话就无此要求). 比如 1 2 32 3 43 ...