POJ2385——Apple Catching
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16248 | Accepted: 8009 |
$Description$
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$
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
$Output$
$Sample~Input$
7 2
2
1
1
2
2
1
1
$Sample~Output$
6
Hint
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
#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的更多相关文章
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- poj2385 Apple Catching(dp状态转移方程推导)
https://vjudge.net/problem/POJ-2385 猛刷简单dp的第一天的第一题. 状态:dp[i][j]表示第i秒移动j次所得的最大苹果数.关键要想到移动j次,根据j的奇偶判断人 ...
- poj2385 - Apple Catching【动态规划】
Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...
- poj2385 Apple Catching
思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- 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 ...
- 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果
3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solv ...
随机推荐
- 一篇不一样的docker原理解析
转自:https://zhuanlan.zhihu.com/p/22382728 https://zhuanlan.zhihu.com/p/22403015 在学习docker的过程中,我发现目前do ...
- 【转载】java 中 String s = new String("abc") 创建了几个对象?!
原文链接点这里,感谢博主分享 答案是两个,现在我们具体的说一下: String s = new String("abc"); 首先我们要明白两个概念,引用变量和对象,对象一般通过n ...
- MySQL/MariaDB数据库忘掉密码解决办法--技术流ken
前言 有些时候我们常常会忘掉一些服务的密码,比如系统密码,我们可以进入救援模式进行修改密码,可参考我之前的博客<Centos7破解密码的两种方法--技术流ken>.但有些时候我们也会忘掉数 ...
- [CSS] input样式定制
input样式 定制一个泥团input,想怎么捏就怎么捏 appearance: none 所有主流浏览器都不支持 appearance 属性. Firefox 支持替代的 -moz-appearan ...
- WPF 水印TextBox WatermarkTextBox
//https://blog.csdn.net/puchitomato/article/details/12248691 转自以上链接,自己添加了Enter响应事件. public class ...
- [MySQL] MVCC 多版本并发控制实现的事务
1.没有一个统一的实现标准,实现了非阻塞的读操作,写操作也只锁定必要的行2.通过保存数据在某个时间点的快照实现的3.典型的有乐观并发控制和悲观并发控制4.innodb的mvcc是每次事务都有递增的版本 ...
- Openresty的同步输出与流式响应
Openresty的同步输出与流式响应 默认情况下, ngx.say和ngx.print都是异步输出的,先来看一个例子: location /test { content_by_lua_block { ...
- ecstore中怎样使用ajax提交数据
//javascript代码 $$(".bb").addEvent('change',function(e){ var order_item_id = this.get('orde ...
- python之循环(增删)内使用list.remove()
dat=['] for item in dat: ': dat.remove(item) print(dat) #按要求是把'0'都删掉的,输出结果是['1', '2', '3', '0'] ?? 首 ...
- openlayers二:添加矢量图形文字
openlayers可方便的在地图上添加圆.多边形.文字等矢量内容,修改这些矢量内容的样式也很简单. 首先需要添加一个向量图层: initVectorLayer: function () { this ...