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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 #include <algorithm>
 6 #include <map>
 7 #include <queue>
 8 #include<vector>
 9 #include <cmath>
10 #define maxn 1010
11 #define maxm 1000000
12 #define mod  1000000000
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 int dp[maxn][40][2];
16 int n,m;
17 int a[maxn][2];
18 int main (){
19     while(cin>>n>>m){
20         for(int i=1;i<=n;++i){
21             int x;
22             cin>>x;
23             a[i][x-1]=1;
24             a[i][2-x]=0;
25         }
26         dp[0][0][0]=0;
27         dp[0][0][1]=0;
28         for(int i=1;i<=n;++i){
29             dp[i][0][0]=dp[i-1][0][0]+a[i][0];
30             dp[i][0][1]=dp[i-1][0][1]+a[i][1];
31         }
32         for(int i=1;i<=n;++i){
33             for(int j=1;j<=m;++j){
34                 dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j-1][1])+a[i][0];
35                 dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j-1][0])+a[i][1];
36             }
37         }
38         int ans=0;
39         for(int i=0;i<=m;++i){
40             for(int j=0;j<2;++j){
41                 ans = max(ans,dp[n][i][j]);
42             }
43         }
44         cout<<ans<<endl;
45     }
46 }

poj 2385,布布扣,bubuko.com

时间: 2024-10-27 07:21:20

poj 2385的相关文章

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【动态规划】

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 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 2385 Apple Catching

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.

DP:Apple Catching(POJ 2385)

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

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.

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.

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

poj 2385 Apple Catching(dp)

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 t