DP:Apple Catching(POJ 2385)

            

                 牛如何吃苹果

  问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的牛最多可以吃到多少个苹果

  首先我们应该很容易想到,这个必须要用DP去做,然后就是考虑怎么储存旧值的问题了,因为树有两棵,然后每个往返状态对应不同的结果,所以我们应该用一个二维矩阵去储存(苹果的个数就不重要了,因为我们只用算到最后,前面的都可以扔掉)。

  然后状态方程应该怎么写呢?也很简单,定义一个状态为dp[i][j]表示在第i棵树的剩余多少次转移机会时的能吃到苹果的最大个数,而牛可以从旁边一棵树走过来也可以是本来就在这棵树旁边

  所以状态转移方程很自然的就是

    dp[i][j]=max(dp[i][j],dp[i-1][j+1])+1 (j<w && i+1>w-j)

    dp[i][j]=dp[i][j]+1;(j=w)

  同时还要注意有一些状态是没有意义的,典型就是i+1<w-j的时候(你想一下苹果掉落的分钟数都比牛跑过来的次数还要少,怎么可能嘛!)

  

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAX(a,b) (a)>(b)?(a):(b)
 4
 5 static int Tree[2][31];
 6
 7 int main(void)
 8 {
 9     int T, W, i, k, Which_Tree, ans = 0;
10     while (~scanf("%d%d", &T, &W))
11     {
12         for (i = 0; i < T; i++)
13         {
14             scanf("%d", &Which_Tree);
15             Tree[Which_Tree - 1][W]++;
16             for (k = W - 1; k >= 0 && i + 1 > W - k; k--)
17                 Tree[Which_Tree - 1][k] =
18                     MAX(Tree[!(Which_Tree - 1)][k + 1] + 1, Tree[Which_Tree - 1][k] + 1);
19         }
20         for (i = 0; i < 2; i++)
21             for (k = 0; k <= W; k++)
22                 ans = MAX(Tree[i][k], ans);
23         printf("%d", ans);
24     }
25
26     return 0;
27 }
时间: 2024-10-07 13:38:22

DP:Apple Catching(POJ 2385)的相关文章

Apple Catching POJ 2385(基础dp)

原题 题目链接 题目分析 基础dp题,按照题意很容易给出dp定义,dp[i][j],表示在i时间内,用j次转移机会得到的最大苹果数.dp转移如下,如果j==0,则dp[i][j]=dp[i-1][j],否则 如果当前位置有苹果dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+1.否则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]).最后在dp[T][j]里找最大值就行了,(0<=j<=W). 代码 1 #include <iostrea

Apple Catching POJ - 2385

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.

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

Apple Catching(POJ 2385)

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9978   Accepted: 4839 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, eac

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 1750双倍经验) ------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm>

POJ2385 Apple Catching 【DP】

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8018   Accepted: 3922 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, eac

Apple Catching(dp)

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9831   Accepted: 4779 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, eac

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

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是从小到大