Apple Catching
 

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.

思路:dp[i][j][k]表示前i个来回最多j次在k(1,2)的那棵树上
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int dp[][][];
int a[];
int main()
{
int x,y,z,i,t;
while(~scanf("%d%d",&x,&y))
{
for(i=;i<=x;i++)
scanf("%d",&a[i]);
for(i=;i<;i++)
for(t=;t<;t++)
for(int j=;j<;j++)
dp[i][t][j]=-inf;
dp[][][]=;
//dp[0][1][2]=0;
memset(dp,,sizeof(dp));
for(i=;i<=x;i++)
{
for(t=;t<=y;t++)
for(int j=;j<=;j++)
{
dp[i][t][j]=max(dp[i][t][j],dp[i-][t][j]);
if(dp[i-][t][j]!=-inf)
{
if(a[i]!=j&&t!=y)
dp[i][t+][a[i]]=max(dp[i][t+][a[i]],dp[i-][t][j]+);
else if(a[i]==j)
dp[i][t][a[i]]=max(dp[i][t][a[i]],dp[i-][t][j]+);
}
}
}
int maxx=;
for(i=;i<=y;i++)
for(t=;t<=;t++)
maxx=max(maxx,dp[x][i][t]);
printf("%d\n",maxx);
}
return ;
}

poj 2385 Apple Catching 基础dp的更多相关文章

  1. POJ 2385 Apple Catching【DP】

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

  2. POJ 2385 Apple Catching ( 经典DP )

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

  3. POJ - 2385 Apple Catching (dp)

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

  4. 【POJ】2385 Apple Catching(dp)

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

  5. poj 2385 Apple Catching(dp)

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

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

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

  7. POJ 2385 Apple Catching(01背包)

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

  8. POJ 2385 Apple Catching

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

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

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

随机推荐

  1. mac shell终端编辑命令行快捷键

    Ctrl + d        删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof) Ctrl + h        退格删除一个字符,相当 ...

  2. python ddt 实现数据驱动一

    ddt 是第三方模块,需安装, pip install ddt DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据) 通常情况下,data中的数据按照一个参数传递给测试用例,如果da ...

  3. Javascript-逻辑或(||)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. UVM中factory机制的使用

    UVM中的factory机制一般用在sequence的重载,尤其是virtual sequence.当Test_case变化时,通过virtual sequence的重载,可以很容易构建新的测试. 因 ...

  5. zw版【转发·台湾nvp系列Delphi例程】HALCON SigmaImage1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SigmaImage1 procedure TForm1.Button1Click(Sender: TObject);var img, im ...

  6. Java设计模式应用——适配器模式

    性能监控系统中,存在告警模块和报表模块,告警结果和报表结果都需要导出. 由于告警开发进度较快,已经实现了excel导出.csv导出.zip导出功能,现在报表需要excel导出.csv导出.pdf导出功 ...

  7. 并发写Btree原理剖析

    OceanBase 0.4的UpdateServer存储引擎使用了一棵可以多线程并发修改的BTree,读写不冲突,由颜然 开发.线上运行稳定. UpdateServer存储引擎采用类leveldb的方 ...

  8. pycharm中内看内建函数的定义

    鼠标方法在内建函数上,Ctrl+B,看内建函数的定义 如果想要看内置函数的具体实现细节,可以到python的lib目录下C:\Python27\Lib\,或者python的官网上 如果要看非内建的函数 ...

  9. python excel操作单元格复制和读取的两种方法

    操作单元格 新建一个sheet, 单元格赋值(两种方法) 单元格A1赋值为’xiaxiaoxu’ 单元格A2赋值为‘xufengchai’ 打印A1和A2单元格的值(两种方法) #coding=utf ...

  10. kafka监控工具

    Kafka Web Conslole Kafka Manager KafkaOffsetMonitor.