POJ_2385 Apple Catching(DP)

题目请点我

题解:

题目符合从多个最优状态得到当前的最优状态,所以是一道DP没错,那么关键是dp数组的定义以及递推方程。

开始的时候按照自己的思路,将连续多次的同一水果掉落合并,

dp数组dp[i][j]定义:在转过i次后到达j位置的最优结果。

递推关系:dp[i][j] = max(dp[i][j],dp[i-1][k]+get(k,j))   (k:i-1~j)  get函数得到k,j之间与当前苹果树对应的果子数目。

本意为果子合并后会简单一些,三层for循环,反倒麻烦了,数据比较弱,还是过了。AC后参考网上的思路,发现会简单很多。

dp数组dp[i][j]定义:在i位置之前转过j次得到的最优结果(刚好相反。。。。)

递推关系:dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]).很好理解,到达位置i有两种可能,一种是呆着没动,另一种是刚转过来,取最优解。如果正好在对应苹果树下,++。简便很多,0ms就过了。

总结下来dp的定义还是要看状态是如何改变的,当前状态由哪几个状态过来,找到合适的dp定义。

参考博客

代码实现:

(自己代码)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long

using namespace std;

const int MAX = 1010;

int T,W;
int res;
int num[MAX];
int dp[35][MAX];
int get(int x,int y);
int main()
{
    scanf("%d%d",&T,&W);
    res = 0;
    int last,tag;
    int sum = 0;
    memset(num,0,sizeof(sum));
    for( int i = 0; i < T; i++ ){
        scanf("%d",&tag);
        if( i == 0 ){
            num[sum]++;
        }
        else{
            if( last == tag ){
                num[sum]++;
            }
            else{
                sum++;
                num[sum]++;
            }
        }
        last = tag;
    }
    memset(dp,0,sizeof(dp));
    for( int i = 0; i <= sum; i++ ){
        dp[0][i] = get(-1,i);
    }
    for( int i = 1; i <= W; i++ ){
        for( int j = i; j <= sum; j++ ){
            for( int k = i-1; k <= j-1; k++ ){
                dp[i][j] = max(dp[i][j],dp[i-1][k]+get(k,j));
            }
        }
        res = max(dp[i][sum],res);
    }
    printf("%d\n",res);
    return 0;
}

//统一定义从下一个位置开始
int get(int x,int y){
    int tmp1 = 0;
    int tmp2 = 0;
    for( int i = x+1; i <= y; i++ ){
        if( (i-x)%2 == 0 ){
            tmp1 += num[i];
        }
        else{
            tmp2 += num[i];
        }
    }
    return max(tmp1,tmp2);
}

(换个思路的另一种代码)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long

using namespace std;

const int MAX = 1010;

int T,W;
int res;
int num[MAX];
int dp[MAX][35];
int main(){
    scanf("%d%d",&T,&W);
    res = 0;
    memset(dp,0,sizeof(dp));
    memset(num,0,sizeof(num));
    for( int i = 1; i <= T; i++ ){
        scanf("%d",&num[i]);
    }
    if( num[1] == 1 ){
        dp[1][0] = 1;
        dp[1][1] = 0;
    }
    else{
        dp[1][1] = 1;
        dp[1][0] = 0;
    }
    for( int i = 2; i <= T; i++ ){
        for( int j = 0; j <= i; j++ ){
            if( j == 0 ){
                dp[i][j] = dp[i-1][j]+num[i]%2;
                continue;
            }
            dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]);
            if( num[i] == j%2+1 ){
                dp[i][j]++;
            }
        }
    }
    for( int i = 0; i <= W; i++ ){
        res = max(res,dp[T][i]);
    }
    printf("%d\n",res);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-23 07:25:15

POJ_2385 Apple Catching(DP)的相关文章

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 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

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 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

POJ2385——Apple Catching

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8062   Accepted: 3951 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

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 18  Solved: 16[Submit][Status][Discuss] Description 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2),每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果从树上落下.但是,由于苹果掉到地上会摔烂,贝茜必须在半空中接住苹果(没

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.