题目
题目链接:http://poj.org/problem?id=2385
题目来源:《挑战》练习题
简要题意:两棵树在 T 分钟内每分钟会有一棵掉苹果,最多换树
W 次,开始在 1 树,求最多接多少苹果。数据范围:
1⩽T⩽1000;1⩽W⩽30
题解
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;
}