標籤:dp
Stupid Tower Defense
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Problem DescriptionFSF is addicted to a stupid tower defense game. The goal of tower defense games is to try to stop enemies from crossing a map by building traps to slow them down and towers which shoot at them as they pass.
The map is a line, which has n unit length. We can build only one tower on each unit length. The enemy takes t seconds on each unit length. And there are 3 kinds of tower in this game: The red tower, the green tower and the blue tower.
The red tower damage on the enemy x points per second when he passes through the tower.
The green tower damage on the enemy y points per second after he passes through the tower.
The blue tower let the enemy go slower than before (that is, the enemy takes more z second to pass an unit length, also, after he passes through the tower.)
Of course, if you are already pass through m green towers, you should have got m*y damage per second. The same, if you are already pass through k blue towers, the enemy should have took t + k*z seconds every unit length.
FSF now wants to know the maximum damage the enemy can get.
InputThere 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)
OutputFor 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.
Sample Input
12 4 3 2 1
Sample Output
Case #1: 12HintFor the first sample, the first tower is blue tower, and the second is red tower. So, the total damage is 4*(1+2)=12 damage points.
題意:給出一條長為n個單位長度的直線,每通過一個單位長度需要t秒。有3種塔,紅塔可以在當前格子每秒造成x點傷害,綠塔可以在之後的格子每秒造成y點傷害,藍塔可以使通過單位長度的時間增加z秒。問如何安排3種塔的順序使得造成的傷害最大,輸出最大傷害值。
分析:如果要安排紅塔,則紅塔在前面沒有在後面造成的傷害大。所以可以枚舉紅塔的數量i,對前n-i座塔進行dp。設dp[i][j]表示前i個單位長度中有j個藍塔造成的最大傷害,則dp[i][j] = max(dp[i-1][j-1] + (i - j) * y * (t + (j - 1) * z), dp[i-1][j] + (i - j - 1) * y * (t + j * z))其中,(i - j) * y * (t + (j - 1) * z)表示i-j個綠塔在第i 個格子造成的傷害,(i - j - 1) * y * (t + j * z)表示i-j-1個綠塔在第i個格子造成的傷害。求出dp[i][j]以後,則紅塔數量為n-i時的總傷害為damage=dp[i][j] + (n - i) * x * (t + j * z) + (n - i) * (t + j * z) * (i - j) * y(後n-i個紅塔造成的傷害加上前i-j個綠塔在後n-i個格子造成的傷害),所以最終的ans=max(ans, damage).
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 1505;typedef __int64 LL;LL dp[N][N];int main(){ LL x, y, z, t, n, i, j; int T, cas = 0; scanf("%d",&T); while(T--) { scanf("%I64d%I64d%I64d%I64d%I64d",&n,&x,&y,&z,&t); memset(dp, 0, sizeof(dp)); LL ans = n * t * x; //全部放紅塔 for(i = 1; i <= n; i++) //前i個單位長度 { for(j = 0; j <= i; j++) // 藍塔數量 { if(j == 0) dp[i][j] = dp[i-1][j] + (i - j - 1) * y * t; else { LL tmp1 = dp[i-1][j-1] + (i - j) * y * (t + (j - 1) * z); //第j個單位長度放藍塔 LL tmp2 = dp[i-1][j] + (i - j - 1) * y * (t + j * z); //第j個單位長度不放藍塔 dp[i][j] = max(tmp1, tmp2); } ans = max(ans, dp[i][j] + (n - i) * x * (t + j * z) + (n - i) * (t + j * z) * (i - j) * y); } } printf("Case #%d: %I64d\n", ++cas, ans); } return 0;}