POJ3176-基础DP

时间:2022-03-08 23:47:41

很基础的dp题。有一头奶牛想接尽量多的苹果,有w此移动机会。

dp[i][w] = max(dp[i-1][w+1] + 能否吃到苹果 ,dp[i-1][w] + 能否吃到苹果)  //从上一分钟是否移动中选出最大值转移

我开始状态转移方程没写错,但是边界条件总是写不好,不能处理0。并不是这样的,刚刚又看了一下代码,加号的优先级高于位运算!!!

想起来了线段树不用i>>1+1而用i>>1|1 了。。。。

后来今天问了妹子,还是专业dp的妹子稳啊,理清了边界条件一遍就A了。

以后写dp要想好状态转移的过程,想好再写。

 #include <algorithm>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> using namespace std; int N,M,T,W;
int apple[],dp[][];
int main()
{
while(~scanf("%d%d",&T,&W))
{
int ans = ;
for(int i=;i<T;i++) scanf("%d",&apple[i]);
memset(dp,,sizeof dp);
dp[][W] = (apple[]-)^(W%);
for(int i=;i<T;i++)
{
//dp[i][0] = (apple[i]-1) + dp[i-1][0];
for(int w = W;w >= ; w--)
{
if(w == W) dp[i][w] = dp[i-][w] + ((apple[i]-)^(w%));
else dp[i][w] = max(dp[i-][w+] + ((apple[i]-)^((w+)%)),dp[i-][w] + ((apple[i]-)^(w%)));
ans = max(ans,dp[i][w]);
}
}
memset(dp,,sizeof dp);
dp[][W] = !((apple[]-)^(W%));
for(int i=;i<T;i++)
{
//dp[i][0] = (apple[i]-1) + dp[i-1][0];
for(int w = W;w >= ; w--)
{
if(w == W) dp[i][w] = dp[i-][w] + !((apple[i]-)^(w%));
else dp[i][w] = max(dp[i-][w+] + !((apple[i]-)^((w+)%)),dp[i-][w] + !((apple[i]-)^(w%)));
ans = max(ans,dp[i][w]);
}
}
printf("%d\n",ans);
}
}