$Apple~Catching$
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16248   Accepted: 8009

$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

 
以上来自百度百科

解法一:
  用一个数组$f_{i,j}$表示第$i$时刻,走了$j$步的最多苹果数。
  状态转移方程为:
    从第一棵苹果树出发:
      $$f_{i,j}=max\{f_{i-1,j},f_{i-1,j-1}\}+(j~\mod~2)= a_i$$
    从第二棵苹果树出发:
      $$f_{i,j}=max\{f_{i-1,j},f_{i-1,j-1}\}+(j~\mod~2)\neq a_i$$
  $code:$
#include <cstdio>
#include <cstring>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std; int read()
{
int x=0,f=1;char c=getchar();
while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
return x*f;
} const int MAXN=1005;
const int MAXM=45;
int n,m;
int a[MAXN];
int f[MAXN][MAXM]; int main()
{
n=read();m=read();
for (int i=1;i<=n;i++) a[i]=read()-1;
memset(f,0,sizeof(f));
for (int i=1;i<=n;i++)
for (int j=0;j<=m;j++)
f[i][j]=max(f[i-1][j],f[i-1][j-1])+((j&1)==a[i]);
int ans=0;
for (int i=0;i<=m;i++)
ans=max(ans,f[n][i]);
memset(f,0,sizeof(f));
for (int i=1;i<=n;i++)
for (int j=0;j<=m;j++)
f[i][j]=max(f[i-1][j],f[i-1][j-1])+((j&1)!=a[i]);
for (int i=0;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
return 0;
}

解法二:

  用一个数组$f_{i,j}$表示移动了$i$步,当前位置在第$j$棵苹果树下的时候的最多苹果数。

  对于第$k$棵树上掉下的一个苹果,要么是之前就已经移动$i$步到了第$k$棵树并等到苹果掉下,或者是移动$i-1$步,到另一棵树下,现在赶到这棵树下。状态转移方程为:

    $$f_{i,j}=max\{f_{i,j},f_{i-1,(j+1)\mod~2}\}+1$$

  $code:$

#include <iostream>
#include <cstdio>
using namespace std; int read()
{
int x=0,f=1;char c=getchar();
while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
return x*f;
}
int n,w,come;
int a[31][2]={0}; int main()
{
n=read();w=read();
for(int i=1;i<=n;i++)
{
come=read()-1;
for (int j=0;j<=w;j++)
a[j][come]=max(a[j][come]+1,a[j-1][(come+1)%2]+1);
}
printf("%d\n",max(a[w][0],a[w][1]));
return 0;
}

POJ2385——Apple Catching的更多相关文章

  1. poj2385 Apple Catching (线性dp)

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

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

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

  3. poj2385 - Apple Catching【动态规划】

    Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...

  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. 一篇不一样的docker原理解析

    转自:https://zhuanlan.zhihu.com/p/22382728 https://zhuanlan.zhihu.com/p/22403015 在学习docker的过程中,我发现目前do ...

  2. 【转载】java 中 String s = new String("abc") 创建了几个对象?!

    原文链接点这里,感谢博主分享 答案是两个,现在我们具体的说一下: String s = new String("abc"); 首先我们要明白两个概念,引用变量和对象,对象一般通过n ...

  3. MySQL/MariaDB数据库忘掉密码解决办法--技术流ken

    前言 有些时候我们常常会忘掉一些服务的密码,比如系统密码,我们可以进入救援模式进行修改密码,可参考我之前的博客<Centos7破解密码的两种方法--技术流ken>.但有些时候我们也会忘掉数 ...

  4. [CSS] input样式定制

    input样式 定制一个泥团input,想怎么捏就怎么捏 appearance: none 所有主流浏览器都不支持 appearance 属性. Firefox 支持替代的 -moz-appearan ...

  5. WPF 水印TextBox WatermarkTextBox

    //https://blog.csdn.net/puchitomato/article/details/12248691 转自以上链接,自己添加了Enter响应事件.    public class ...

  6. [MySQL] MVCC 多版本并发控制实现的事务

    1.没有一个统一的实现标准,实现了非阻塞的读操作,写操作也只锁定必要的行2.通过保存数据在某个时间点的快照实现的3.典型的有乐观并发控制和悲观并发控制4.innodb的mvcc是每次事务都有递增的版本 ...

  7. Openresty的同步输出与流式响应

    Openresty的同步输出与流式响应 默认情况下, ngx.say和ngx.print都是异步输出的,先来看一个例子: location /test { content_by_lua_block { ...

  8. ecstore中怎样使用ajax提交数据

    //javascript代码 $$(".bb").addEvent('change',function(e){ var order_item_id = this.get('orde ...

  9. python之循环(增删)内使用list.remove()

    dat=['] for item in dat: ': dat.remove(item) print(dat) #按要求是把'0'都删掉的,输出结果是['1', '2', '3', '0'] ?? 首 ...

  10. openlayers二:添加矢量图形文字

    openlayers可方便的在地图上添加圆.多边形.文字等矢量内容,修改这些矢量内容的样式也很简单. 首先需要添加一个向量图层: initVectorLayer: function () { this ...