poj--2385--Apple Catching(状态dp)
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
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
* 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.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1010][1010][2];
int num[1010];
int main()
{
int n,time;
int maxx=-1000;
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&time);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
{
dp[i][0][0]=dp[i-1][0][0]+(num[i]==1);
dp[i][0][1]=dp[i-1][0][1]+(num[i]==2);
for(int j=1;j<=time;j++)
{
dp[i][j][0]=max(dp[i-1][j-1][1],dp[i-1][j][0])+(num[i]==1);
dp[i][j][1]=max(dp[i-1][j-1][0],dp[i-1][j][1])+(num[i]==2);
maxx=max(maxx,max(dp[i][j][0],dp[i][j][1]));
}
}
printf("%d\n",maxx);
return 0;
}
poj--2385--Apple Catching(状态dp)的更多相关文章
- 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 )
题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...
- POJ 2385 Apple Catching【DP】
题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...
- POJ - 2385 Apple Catching (dp)
题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- poj 2385 Apple Catching(dp)
Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...
- poj 2385 Apple Catching(记录结果再利用的动态规划)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...
- POJ 2385 Apple Catching
比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...
- POJ 2385 Apple Catching(01背包)
01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...
- 动态规划:POJ No 2385 Apple Catching
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...
随机推荐
- 我一不小心把ubuntu里的ps这样的命令删掉了,应该怎么重装呢
sudo apt-get --reinstall install procps
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- nyoj--233--Sort it (水题)
Sort it 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 You want to processe a sequence of n distinct integer ...
- 我网站用session做的登录,为什么清除浏览器数据后还是得重新登录?session是存在服务器上的。
答案一: 你清除了浏览器数据,相当于把cookie也清了,那么你的sessionId也就没有了,所以你再次请求的时候服务器无法根据你携带的sessionid来获取对应的session,所以说需要重新登 ...
- 固定执行计划-使用coe_xfr_sql_profile
一.历史执行计划固定 历史的执行计划找到一个合理的执行计划进行绑定 1. 存在多个执行计划的语句,按照索引是比较合适的,FULL SCAN不合适 select * from scott.emp whe ...
- linux批处理笔记
最近不得不用到Linux批处理,于是把要用到的程序反复研究了一下. #!/bin/bash是指此脚本使用/bin/bash来解释执行. -le -ge分别是小于和大于,这个倒是和latex里面的命令很 ...
- android之handler机制深入解析
一.android中需要另开线程处理耗时.网络的任务,但是有必须要在UI线程中修改组件.这样做是为了: ①只能在UI线程中修改组件,避免了多线程造成组件显示混乱 ②不使用加锁策略是为了提高性能,因为a ...
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
- servlet中Session的用法
## (1)什么是Session? 服务器端为了保存用户的状态而创建的一个特殊的对象(即session对象). 当浏览器第一次访问服务器时,服务器会创建session对象(该 ...
- NOI2018 你的名字 后缀自动机_线段树合并_可持久化
相当复杂的一道题,同样也相当优美.考察的知识点很多:权值线段树的可持久化合并,后缀自动机,后缀树... 考虑 $68pts$ $l=1,r=|s|$的数据:这部分相对好做一些,不过思维难度对我来说已 ...