Apple Catching(POJ 2385)

时间:2022-03-13 12:48:24
Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9978   Accepted: 4839

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int i,j,t,n,Max=;
int dp[+][];
int a[+];
freopen("in.txt","r",stdin);
scanf("%d%d",&t,&n);
for(i=;i<=t;i++)
scanf("%d",&a[i]);
for(i=;i<=t;i++)
{
dp[i][]=dp[i-][]+-a[i]; //dp[i][j]={第i分钟走了j步所摘到的苹果}
for(j=;j<=n;j++)
{
if(j%) //如果为奇数,说明走到了2树
dp[i][j]=max(dp[i-][j],dp[i-][j-])+a[i]-; //取前一分钟的最大值
else
dp[i][j]=max(dp[i-][j],dp[i-][j-])+-a[i];
}
}
for(i=;i<=n;i++)
Max=max(dp[t][i],Max);
printf("%d\n",Max); }

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

  1. poj2385 Apple Catching (线性dp)

    题目传送门 Apple Catching Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 154 ...

  2. POJ 2385 Apple Catching(01背包)

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

  3. 【POJ - 2385】Apple Catching(动态规划)

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

  4. 【POJ】2385 Apple Catching(dp)

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

  5. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  6. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  7. 广大暑假训练1(poj 2488) A Knight&&num;39&semi;s Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  8. Games&colon;取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  9. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

随机推荐

  1. Java Web之会话管理一&colon; 使用Cookie进行会话管理

    一.Cookie的概念 Cookie(会话)可以简单的理解为:用户开一个浏览器,点击多个链接,访问服务器多个web资源,然后关闭浏览器,整个过程称为一个会话. 二.会话过程中解决的问题 用户在使用浏览 ...

  2. 浏览器获取ip地址

    /** * 获取浏览器的ip地址 * @param request * @return */ public static String getIP(HttpServletRequest request ...

  3. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. 第一部分 代码组织概念,集成开发环境&lpar;IDE&rpar;

    代码组织概念 主要是代码文件,项目和解决方案. 解决方案(.sln)包含多个项目(.csproj),一个项目又包含多个文件(.cs). 集成开发环境(IDE): 由编辑.编译.调试,以及用户图形界面, ...

  5. 转载一篇makefile,说的很详细

    March 3, 2015 8:19 PM 原文见:https://www.cnblogs.com/OpenShiFt/p/4313351.html Makefile 文件的编写 学习前的准备 需要准 ...

  6. adapter&period;notifydatasetchanged&lpar;&rpar;没有效果

    项目中有个列表的处理,通过一个参数判断是下拉刷新数据还是加载更多数据,结果下拉刷新就是显示不出来界面,数据是有,就开始searching~,搜出很多相关问题,大意如下: 1 当数据源发生变化的时候,我 ...

  7. ELK&lpar;elasticsearch&plus;kibana&plus;logstash&rpar;搜索引擎&lpar;一&rpar;: 环境搭建

    1.ELK简介 这里简单介绍一下elk架构中的各个组件,关于elk的详细介绍的请自行百度 Elasticsearch是个开源分布式搜索引擎,是整个ELK架构的核心 Logstash可以对数据进行收集. ...

  8. jQuery如何退出each循环 和如何退出function函数

    1.在函数内部使用return false是跳出function; 2.在each的回调函数中使用return false,是跳出each循环;return true 进入下一个循环: 3.break ...

  9. 可能是最早的学习Android N新特性的文章

    可能是最早的学习Android N新特性的文章 Google在今天放出了Android N开发者预览版.Android N支持Nexus6及以上的设备.5太子Nexus5不再得到更新. Android ...

  10. English trip Spoken English &amp&semi; Word List(updating&period;&period;&period;)

    Word List 词汇 Square  英 [skweə]  美 [skwɛr]  adj. 平方的:正方形的:直角的:正直的. 使成方形:与…一致 vi. 一致:成方形 n. 平方:广场:正方形 ...