dp[i][j]表示到了第i步放了j个减速,造成的伤害。我们用贪心的策略把造成一段伤害的放在最后面,造成持续伤害的与减速放在前i个中这样得到的伤害是最高的。
所以前(i,j)中的伤害为dp[i][j] = max(dp[i-1][j]+(j*z+t)*(max(0LL, i-1-j))*y, dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y);
每次造成的伤害就为:dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y)。然后取一个最大值,就是伤害的最大值了。
1 2 4 3 2 1
Case #1: 12
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
using namespace std;
const int maxn = 2010;
LL dp[maxn][maxn];
int main()
{
int T;
cin >>T;
int Case = 1;
while(T--)
{
LL n, x, y, z, t;
cin >>n>>x>>y>>z>>t;
memset(dp, 0, sizeof(dp));
LL ans = n*t*x;
for(LL i = 1; i <= n; i++)
{
for(LL j = 0; j <= i; j++)
{
if(j == 0) dp[i][j] = dp[i-1][j]+t*(i-1-j)*y;
else dp[i][j] = max(dp[i-1][j]+(j*z+t)*(max(0LL, i-1-j))*y, dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y);
ans = max(ans, dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y));
}
}
cout<<"Case #"<<Case++<<": "<<ans<<endl;
}
return 0;
}
HDU 4939 Stupid Tower Defense(dp+贪心),布布扣,bubuko.com
HDU 4939 Stupid Tower Defense(dp+贪心)
原文:http://blog.csdn.net/xu12110501127/article/details/38553305