$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. Litepal【开源数据库ORM框架】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 好用的数据库框架. 效果图 代码分析 本篇主要是整理Litepal的引入和增删改查的简单操作,具体使用请阅读参考资料. 使用步骤 一 ...

  2. RecyclerSwipeAdapterDemo【使用AndroidSwipeLayout用于列表项侧滑功能】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录AndroidSwipeLayout搭配Recyclerview实现列表项侧滑功能. 效果图 代码分析 适配器类中设置Swipe ...

  3. springboot~使用docker构建gradle项目

    这是一篇关系到四个知识点的文章,分别是java,docker,springboot和gradle,我们希望在java环境下,使用springboot框架,通过gradle去构建项目,然后把项目部署和运 ...

  4. SpringBoot简单打包部署(附工程)

    前言 本文主要介绍SpringBoot的一些打包事项和项目部署以及在其中遇到一些问题的解决方案. SpringBoot打包 在SpringBoot打包这块,我们就用之前的一个web项目来进行打包. 首 ...

  5. tablednd onDrap 方法不调用

    场景 使用 tablednd 插件时,onDrap 方法不调用 解决 给tr标签加 id 属性

  6. http协议、模块、express框架以及路由器、中间件和mysql模块

    一.http协议 是浏览器和web服务器之间的通信协议 1.通用头信息 request url:请求的url,向服务器请求的数据 request method:请求的方式   get.post sta ...

  7. 模板引擎artTemplate的使用

    1.引入template文件 <script src = js/template-native.js></script> 2.写模板 <script type=" ...

  8. noi.ac#309 Mas的童年(子集乱搞)

    题意 题目链接 Sol 记\(s_i\)表示前\(i\)个数的前缀异或和,我们每次相当于要找一个\(j\)满足\(0 < j < i\)且\((s_i \oplus s_j) + s_j\ ...

  9. Odoo:全球第一免费开源ERP库龄表的简单实现方法(无需二开)

    问题背景 希望查看库龄超过30天的货物,该如何实现?此种简单数据查询需要二开吗? 解决方案 方法一:Stock Quant列表视图增加过滤器 <filter string="库龄超30 ...

  10. openlayers三:添加图片和图标

    openlayers添加图片是指: 添加在地图上的图片会跟随地图同步放大缩小 而添加图标是指: 添加在地图上的图片不会跟随地图同步放大缩小 添加图片: 首先初始化图片图层: initImageLaye ...