Apple Catching(dp)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9831 | Accepted: 4779 |
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.
dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+x-1;
else
dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+(x&1);
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ");
#define puts puts("");
typedef long long LL;
const int MAXN=1010;
int dp[35][MAXN];
int main(){
int T,W;
while(~scanf("%d%d",&T,&W)){
mem(dp,0);
int x;
for(int i=1;i<=T;i++){
SI(x);
dp[0][i]=dp[0][i-1]+(x&1);
for(int j=1;j<=W;j++){
if(j&1)
dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+x-1;
else
dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+(x&1);
}
}
int ans=0;
for(int i=0;i<=W;i++)ans=max(ans,dp[i][T]);
printf("%d\n",ans);
}
return 0;
}
Apple Catching(dp)的更多相关文章
- poj 2385 Apple Catching(dp)
Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...
- 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 ...
- poj 2385 Apple Catching 基础dp
Apple Catching Description It is a little known fact that cows love apples. Farmer John has two ap ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- POJ 2385 Apple Catching【DP】
题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果
3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solv ...
- bzoj 3384: [Usaco2004 Nov]Apple Catching 接苹果
双倍经验题... -->1750 dp!! 3384: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec Memory Limit: 12 ...
随机推荐
- property、synthesize、id
1.@property int age; 在编译器情况下会自动编译展开为: <age在setter中首字母大写,点语法为p.age> - (void)setAge:(int)age; - ...
- tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中
1.首先还是要将u-boot写入SD卡中从SD卡启动. 使用读卡器将SD插入电脑中,使用umount卸载u盘, fdisk -l显示其挂载点为 /dev/sdb1 切换到/home/bunfly/im ...
- 【Linux指令】使用中学习(一)
sed指令: 应用:对于大文件,比如10G的大文件,我遇到的是导出的数据库.sql文件,想要使用vim修改几乎是不可能的,用sed指令可以在不打开文件的情况下修改文件,下面是一些具体用法 删除文件特定 ...
- ProcessBuilder 和 Runtime(转)
ProcessBuilder.start() 和 Runtime.exec() 方法都被用来创建一个操作系统进程(执行命令行操作),并返回 Process 子类的一个实例,该实例可用来控制进程状态并获 ...
- java学习之三种常用设计模式
一.适配器设计模式 简单来说,就是通过一个间接类来选择性的来覆写一个接口 interface Window{ public void open() ; // 打开窗口 public void clos ...
- http://docs.aliyun.com/#/rds/best-practices/collocation&security
http://docs.aliyun.com/#/rds/best-practices/collocation&security
- [Oracle] Listener的动态注册
在有Oracle Listener的动态注册之前,采用的是静态注册,所谓静态注册是指Oracle实例在启动时,读取listener.ora里的配置,然后注册到Listener,它主要有两个缺点: 1. ...
- C++_const
//const在C不可以初始化数组 //const在C++可以用来初始化数组 #include <iostream> using namespace std; void main() { ...
- Baskets of Gold Coins
Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
新的keyword auto C++11中引入auto第一种作用是为了自己主动类型推导 auto的自己主动类型推导,用于从初始化表达式中判断出变量的数据类型.通过auto的自己主动类型推导.能够大大简 ...