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

USACO 2004 November

思路:首先,我们定义了一个num数组,其中num[0][i]表示第i分钟时第一颗树掉苹果,num[1][i]表示第i分钟时第二颗树掉苹果。

我们可以推导

1. 当j==0时,也就是说没有移动一次,dp[i][j]=dp[i-1][j]+num[0][i];

2. 当j!=0时, dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];其中num数组是判断他要去的那棵树是否有苹果掉下来。

大概思路就是这样,具体细节看代码。

#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int maxn=1005;
int num[2][maxn],dp[maxn][35]; int main()
{
int t,w;
scanf("%d%d",&t,&w);
memset(num,0,sizeof(num));
for(int i=1;i<=t;++i)
{
int x;
scanf("%d",&x);
if(x==1) //第一颗树掉苹果
num[0][i]=1;
else //第二颗树掉苹果
num[1][i]=1;
}
memset(dp,0,sizeof(dp));
dp[1][0]=num[0][1],dp[1][1]=num[1][1]; //初始化,临界条件
for(int i=2;i<=t;++i)
{
for(int j=0;j<=w && j<=i;++j)
{
if(j==0)
dp[i][j]=dp[i-1][j]+num[0][i];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];
} //转移与不转移比较,选择较大值
}
printf("%d\n",dp[t][w]);
return 0;
}

poj2385 - Apple Catching【动态规划】的更多相关文章

  1. POJ2385——Apple Catching

                                                $Apple~Catching$ Time Limit: 1000MS   Memory Limit: 6553 ...

  2. poj2385 Apple Catching (线性dp)

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

  3. poj2385 Apple Catching(dp状态转移方程推导)

    https://vjudge.net/problem/POJ-2385 猛刷简单dp的第一天的第一题. 状态:dp[i][j]表示第i秒移动j次所得的最大苹果数.关键要想到移动j次,根据j的奇偶判断人 ...

  4. poj2385 Apple Catching

    思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...

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

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

  6. Apple Catching(POJ 2385)

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

  7. Apple Catching(dp)

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

  8. 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 ...

  9. 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

    3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 18  Solv ...

随机推荐

  1. Uva 11151 - Longest Palindrome

    A palindrome is a string that reads the same from the left as it does from the right. For example, I ...

  2. 【POJ 2559】 Largest Rectangle in a Histogram

    [题目链接] http://poj.org/problem?id=2559 [算法] 单调栈 [代码] #include <algorithm> #include <bitset&g ...

  3. P5180 【模板】支配树

    这个题乱七八糟的,和之前的灭绝树有点像,但是不一样.那个是DAG,这个是有向图.简单步骤就是先求出来dfs序,然后求出半支配点(?),然后通过这个求支配点. 算法不是很理解,先放在这. 题干: 题目背 ...

  4. web跨域问题回顾

    晚上看spring web源码时看到了cors包,查了一下原来是在4.2之后新加的用来更方便让web应用服务支持cors协议的.于是有了下面几个问题. web跨域问题的起源是因为浏览器为了安全而遵循的 ...

  5. bzoj1833

    http://www.lydsy.com/JudgeOnline/problem.php?id=1833 2.5个小时就花在这上面了... 水到200题了...然并卵,天天做水题有什么前途... #i ...

  6. [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Linux搭建tomcat文件服务器

    Linux搭建tomcat文件服务器 Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar. ...

  8. RocketMQ(2)

    1. 消费端集群消费(负载均衡) 示例代码: /** * Producer,发送消息 * */ public class Producer { public static void main(Stri ...

  9. jmeter关联、下载文件、简单压测

    关联 一.什么是关联 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 简单地说就是在测试过程中有些数据的值会经常发生变化,要获取并使用这些数据,把这个动态的信 ...

  10. 51nod 1222 莫比乌斯反演

    思路: yhx找的反演题 题解已经烂大街了 #pragma GCC optimize("O3") //By SiriusRen #include <bits/stdc++.h ...