Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8759   Accepted: 4264

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

简单dp。
比赛的时候一直没改出来,主要是感觉样例过了,就应该是小问题,其实不然,样例毕竟坑啊。
这题其实一开始思路没错,但是没想清楚,就开始敲,敲完之后觉得时出了小问题,就一直搁那瞅,到最后也没醒悟。比完之后过了一段时间做,竟然还犯了同样的错误,我真是好记性啊(平时也没感觉粗来啊)。不过这次终于改过来了。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1005
int d[MAXN][][];
int tree[MAXN];
int main()
{
int t, w;
memset(d, , sizeof(d));
scanf("%d%d", &t, &w);
for(int i = ; i <= t; i++) {
scanf("%d", &tree[i]);
tree[i]--;
}
for(int i = ; i <= t; i++) {
d[i][][tree[i]] = d[i - ][][tree[i]] + ,
d[i][][!tree[i]] = d[i - ][][!tree[i]];
for(int j = ; j <= w; j++)
d[i][j][tree[i]] = max(d[i - ][j - ][!tree[i]], d[i - ][j][tree[i]]) + ,
d[i][j][!tree[i]] = d[i - ][j][!tree[i]];
}
int maxn = -;
for(int i = ; i <= w; i++) maxn = max(maxn, max(d[t][i][], d[t][i][]));
printf("%d", maxn);
return ;
}

A-Apple Catching(POJ 2385)的更多相关文章

  1. DP:Apple Catching(POJ 2385)

    牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...

  2. Day9 - A - Apple Catching POJ - 2385

    Description 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可 ...

  3. Apple Catching(POJ 2385)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9978   Accepted: 4839 De ...

  4. poj 2385 Apple Catching 基础dp

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

  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(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  7. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  8. Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9831   Accepted: 4779 De ...

  9. BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )

    dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...

随机推荐

  1. observer观察者模式

    观察者模式(有时又被称为发布-订阅Subscribe>模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态上发生变化时,会通知所有观察者对象,让 ...

  2. python_way ,day23 API

    python_way ,day23 1.api认证  .api加密动态请求 2.自定义session 一.api认证 首先提供api的公司,如支付宝,微信,都会给你一个用户id,然后还会让你下一个SD ...

  3. STORM_0008_Structure-of-the-codebase_Storm的代码库的结构

    http://storm.apache.org/releases/1.0.1/Structure-of-the-codebase.html Structure of the codebase 源码分成 ...

  4. hdu 5137 How Many Maos Does the Guanxi Worth 最短路 spfa

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  5. 数据库mysql中having 和where的区别

    having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选. ...

  6. js分组排序算法, OrderBy

    由于项目中需要对数据进行分组排序,类似于sql中 order by column1,column2....  实现的关键是 分组排序,第一个column1,排序完成之后,对其分组,然后按照column ...

  7. nodejs学习笔记<三>关于路由(url)

    在网站开发中,路由的设置非常关键.nodejs对路由处理封装了一个比较全面的模块. 来认识下url模块 1)在命令行(cmd)可以直接 node —> url 可直接查看url模块的所有方法. ...

  8. linux之echo命令

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个提示 ...

  9. laravel captcha

    https://packagist.org/packages/gregwar/captcha

  10. android瀑布流效果(仿蘑菇街)

    Android 转载分享(10)  我们还是来看一款示例:(蘑菇街)           看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此. ...