想了想,人生真是艰难啊。
题意:
一个一维塔防游戏,一条路有n个单位长度,敌人需要t秒通过一个单位长度,你可以在每个单位长度放置一个塔,你有三种塔,第一种攻击塔,对正在通过它的敌人每秒造成x点伤害,第二种毒塔,对通过了它的敌人每秒造成y点伤害,第三种冰塔,通过了它的敌人通过每单位长度需要多花z秒时间。问最大能对一个敌人造成多少点伤害。
Input
There are multiply test cases.
The first line contains an integer T (T<=100), indicates the number of cases.
Each test only contain 5 integers n, x, y, z, t (2<=n<=1500,0<=x, y, z<=60000,1<=t<=3)
The first line contains an integer T (T<=100), indicates the number of cases.
Each test only contain 5 integers n, x, y, z, t (2<=n<=1500,0<=x, y, z<=60000,1<=t<=3)
Output
For each case, you should output "Case #C: " first, where C indicates the case number and counts from 1. Then output the answer. For each test only one line which have one integer, the answer to this question.
这题一开始一看就是DP,然后仔细一看得三维DP,后来同学发现攻击塔肯定放在其他两种塔后面,并且肯定摆满,本来他觉得毒塔肯定在冰塔前面,但是我觉得证不出来的,而且有可能会毒塔冰塔交错,还是得DP。然后我就想出了二维DP的方法(虽然最后数组用了三维的),但是有个地方没定义好WA了6发哦凑。。。队友在宿舍里用另外一种姿势做这个题,WA了两发0ms。。。于是我们队WA了8发这个题。。。。
dp[2][i][j]
1) 第一维仅仅是为了计算方便,想明白了DP过程应该也可以不要第一维,用O(1)的时间就可以求出来需要的量。第一维为1表示dp存储的量是路上i位置之后的所有单位长度都布满攻击塔,0表示i+1位置什么都没有放(但是仍然受到之前的冰塔和毒塔的影响),之后的全放了攻击塔。
2) 第二维是当前位置,位置从1开始。
3) 第三维表示1-i位置一共有多少个毒塔,其余全是冰塔。
这样的话答案就是最大的dp[1][i][j],第一维为0仅仅是为了方便计算状态转移,状态转移方程好恶心的,变量那么多,但是定义清楚了的话就是模拟的问题了,具体看代码吧。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 1520
long long n,x,y,z,t;
long long dp[2][mxn][mxn];
int main(){
int cs;
scanf("%d",&cs);
for(int CS=1;CS<=cs;++CS){
scanf("%lld%lld%lld%lld%lld",&n,&x,&y,&z,&t);
memset(dp,0,sizeof(dp));
dp[1][0][0]=x*n*t;
dp[0][0][0]=x*(n-1)*t;
long long ans=dp[1][0][0];
for(long long i=1;i<=n;++i)
for(long long j=0;j<=i;++j){
if(j) dp[1][i][j]=dp[0][i-1][j-1]+(n-i)*(t+(i-j)*z)*y;
if(j!=i) dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+(n-i)*z*(x+y*j));
if(j){
dp[0][i][j]=dp[0][i-1][j-1]-x*(t+z*(i-j));
dp[0][i][j]+=(n-i)*(t+z*(i-j))*y;
}
if(j!=i) dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j]-x*(t+(i-1-j)*z)+x*(n-i-1)*z+y*j*(n-i)*z);
ans=max(ans,dp[1][i][j]);
}
printf("Case #%d: %lld\n",CS,ans);
}
return 0;
}