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 <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18
19 int dp[2000][100];
20 int T[2000];
21
22 int main()
23 {
24 //    freopen("black.in","r",stdin);
25 //    freopen("black.out","w",stdout);
26     int t,w;
27     cin>>t>>w;
28     for(int i=1;i<=t;i++) cin>>T[i];
29     for(int i=1;i<=t;i++)
30     for(int j=0;j<=w;j++)
31     {
32         if(j<=i)
33         {
34             if(!j)
35             {
36                 if(T[i]==1) dp[i][j]=dp[i-1][j]+1;
37                 else dp[i][j]=dp[i-1][j];
38             }
39             else
40             {
41                 if(T[i]==(j&1)+1) dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+1;
42                 else dp[i][j]=max(dp[i-1][j-1],dp[i-1][j]);
43             }
44         }
45     }
46     int ans=0;
47     for(int i=0;i<=w;i++) ans=max(ans,dp[t][i]);
48     cout<<ans<<endl;
49     return 0;
50 } 

原文地址:https://www.cnblogs.com/VBEL/p/11405066.html

时间: 2024-10-11 22:42:11

Apple Catching POJ 2385(基础dp)的相关文章

DP:Apple Catching(POJ 2385)

牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的牛最多可以吃到多少个苹果 首先我们应该很容易想到,这个必须要用DP去做,然后就是考虑怎么储存旧值的问题了,因为树有两棵,然后每个往返状态对应不同的结果,所以我们应该用一个二维矩阵去储存(苹果的个数就不重要了,因为我们只用算到最后,前面的都可以扔掉). 然后状态方程应该怎么写呢?也很简单,定义一个状态

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

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each

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

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] Jury Compromise 题目 In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is do