poj 2385 Apple Catching dp

题目

题目链接:http://poj.org/problem?id=2385

题目来源:《挑战》练习题

简要题意:两棵树在 T 分钟内每分钟会有一棵掉苹果,最多换树W次,开始在 1 树,求最多接多少苹果。

数据范围:1T1000;1W30

题解

dp[i][j][k] 代表 i 分钟换树j次停在 k <script type="math/tex" id="MathJax-Element-294">k</script>号树的最大价值。

每个状态又两个来源要么前一次停在原地要么,要么前一次换一棵树。

实现

边界处理要注意,要注意状态的合法性。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int T = 1005;
const int W = 35;
const int ST = 2;

int dp[T][W][ST];

int main()
{
    int t, w, st, op;
    scanf("%d%d", &t, &w);
    memset(dp, -1, sizeof dp);

    dp[0][0][0] = 0;
    for (int i = 1; i <= t; i++) {
        scanf("%d", &st);
        st--;
        op = st^1;
        dp[i][0][0] = dp[i-1][0][0] + (st==0);
        for (int j = 1; j <= w; j++) {
            dp[i][j][st] = max(dp[i-1][j][st], dp[i-1][j-1][op]);
            if (dp[i][j][st] > -1) dp[i][j][st]++;
            dp[i][j][op] = max(dp[i-1][j][op], dp[i-1][j-1][st]);
        }
    }

    int ans = 0;
    for (int i = 0; i <= w; i++) {
        ans = max(ans, max(dp[t][i][0], dp[t][i][1]));
    }
    printf("%d\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值