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 ...
随机推荐
- Java~关于开发工具和包包
大叔也学java了,距离上学时接触的java已经有10多年了,看着确实有些陌生了,不过还是希望学学,感受一下这个当今最牛X的语言!开发工具IDE对于一个语言来说是很必要的,就是Csharp使用vs一样 ...
- Java基础系列-equals方法和hashCode方法
原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述 equals方法和hashCode方法都是有Object类定义的. publi ...
- DSAPI 键盘鼠标钩子
通常,说到Hook键盘鼠标,总需要一大堆代码,涉及各种不明白的API.而在DSAPI中,可以说已经把勾子简化到不能再简化的地步.甚至不需要任何示例代码即会使用.那么如何实现呢? Private Wit ...
- 【译】《C# Tips -- Write Better C#》
[译]<C# Tips -- Write Better C#> <C# 奇淫巧技 -- 编写更优雅的 C#> 目录 介绍(Introduction) 第一部分:各种奇淫巧技(P ...
- h5页面 video暂停播放 视频控件 以及当前页面只有一个可以播放效果
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- 我的世界 ParaCraft 结合开源地图 OpenStreetMap 生成3D校园的方法简介
我的世界ParaCraft结合开源地图OpenStreetMap生成3D校园的方法简介 版本1.0 日期2019.2.3 作者Ray (82735589@qq.com) www.TimeGIS.com ...
- 在 Angular 8 中,我们可以期待些什么
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 本文由葡萄城翻译并发布 --- Angular 作为一款优秀的前端框架,自诞生之日起,就致力于面向前端开发者 ...
- Chromium被用于Microsoft Edge与ChakraCore的未来【译】
注:英语不好,力求大概能懂.持笔人是:Limin Zhu,好像是中国人,但是没有提供中文版本. 大家好,ChakraCore的朋友们: 昨天,微软公布,Microsoft Edge桌面浏览器采用Chr ...
- VS Code常用快捷键大全
常用 General 按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick O ...
- 如何取消Microsoft账户登录电脑
手贱用Microsoft账户登录了一下笔记本里面的日历,TNND微软直接就把你电脑的登录账户直接改成了微软账户,花了1个小时才搞回去. 步骤如下: 0--脑残微软的设计,点了下日历,弹出下面这个,绝对 ...