poj 2385【动态规划】

poj 2385

Apple Catching

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14007   Accepted: 6838

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.

题意:有树1和树2,他们在每一时刻只有一棵树落苹果,给定时间T,可在两棵树间移动的最大次数W,初始时站在树1下面。求能接到苹果数的最大值。

题解:定义状态dp[i][j]表示在i时刻移动了j次时能得到的最大苹果数。状态的含义很重要!则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+(移动j次后对应的树正好落苹果?1:0)

  方程想出来了,但是细节,边界处理,循环上还不会。。。显然移动次数为0时状态只能由上一个时间不移动的状态转移过来,即dp[i][0]=dp[i-1][0]+app[i]%2(初始时在树1下),而且有在第一时刻时若树1落苹果则dp[1][0]=1,否则dp[1][1]=1。由于是最多能移动W次并不是一定要移动W次,所以最后在0~W次结果中取最大值。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int dp[1050][50];
 8 int app[1050];
 9
10 int main()
11 {
12     int T,W;
13     scanf("%d%d",&T,&W);
14     memset(dp,0,sizeof(dp));
15     for(int i=1;i<=T;i++){
16         scanf("%d",&app[i]);
17     }
18     if(app[1]==1) dp[1][0]=1;
19     else dp[1][1]=1;
20     for(int i=2;i<=T;i++){
21         dp[i][0]=dp[i-1][0]+app[i]%2;
22         for(int j=1;j<=W;j++){
23             dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
24             if(j%2+1==app[i]) dp[i][j]++;
25         }
26     }
27     int ans=0;
28     for(int i=0;i<=W;i++)
29         ans=max(ans,dp[T][i]);
30     printf("%d\n",ans);
31     return 0;
32 }

原文地址:https://www.cnblogs.com/zxhyxiao/p/8459253.html

时间: 2024-10-10 20:36:47

poj 2385【动态规划】的相关文章

poj 2385

题目大意:有两颗苹果树,每一秒会有一颗掉落一个苹果(一共n秒),问在限制最多转换(从一颗走到另一颗)m次下最多能得到多少苹果. 分析: dp[i][j][k]表示第i秒转换了j次当前在第k棵树下得到的苹果数最大值 显然只与上一秒的状态有关 dp[i][j][k]=max{dp[i-1][j][k],dp[i-1][j-1][1-k]}+a[i][k] {a[i][k]表示第i秒第k棵树是否有苹果} 那么答案就是max{dp[n][j][k]} 空间上可以优化,可以把第一维拿掉只要保证i是从小到大

poj 1458 动态规划DP

//  poj 1458  zoj 1733  最长公共子序列  DP #include <iostream>#include <string.h>#define N 1005using namespace std ;char  s1[N],s2[N];   int dp[N][N];int max(int a,int b)   {    return a>b ? a : b ;  }void f(int n,int m){   int i,j;    for (i=0; i

POJ 2385 Apple Catching 接苹果 DP

题目链接:POJ 2385 Apple Catching Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7858   Accepted: 3846 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently number

poj 2385 Apple Catching(记录结果再利用的动态规划)

传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时间内在两个苹果树间切换,但每一时刻只能切换一下: 求在1~T时刻,Bessie在最多可以切换W次的前提下最多可以获得多少苹果? 题解: 定义变量dp[ i ][ j ] : 前 i 时刻,移动 j 步所获得的最大的苹果数量: 据此写出状态转移方程: 如何判断在i处是否的到苹果呢? ①如果dp[i-1

POJ 1661 动态规划 Help Jimmy

动态规划,意思简洁明了. 排序,n^2做法. 每次检测可行的长条,最高的永远是起始点. dp[i][0]表示第i个长条的左边下去最短的时间. dp[i][1]表示第i个长条的右边下去最短的时间. #include <cstdio>#include <iostream>#include <vector>#include <cmath>#include <set>#include <map>#include <queue>#i

POJ 2385 Apple Catching(简单DP)

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.

M - Help Jimmy POJ 1661 ( 动态规划 )

M - Help Jimmy Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒.当Ji

O - Treats for the Cows POJ 3186 ( 动态规划+区间 )

O - Treats for the Cows Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk.

poj 1579(动态规划初探之记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b