HDU 4939 Stupid Tower Defense (Greedy + dp)
HDU Stupid Tower Defense
Question Link
There are some towers where the Red Tower can attack him. After the Green Tower can attack him, the blue tower can slow down and put the tower on 1-N to maximize the damage.
Thought: At first, we thought we were greedy. The Green Tower was at the beginning, and the blue tower was in the middle, and the red tower was at the end. The result was actually wrong.
However, the Red Tower is definitely at the end. This clearly does not prove much. It is a greedy idea.
Then, put dp [I] [j] to I, and there is j Green Tower in front of it to go to the status transfer.
Code:
#include
#include
#include using namespace std;const int N = 1505;typedef long long ll;int T;ll n, x, y, z, t;ll dp[N][N];ll solve() { ll ans = n * x * t; dp[0][0] = 0; for (ll i = 1; i <= n; i++) {for (ll j = 0; j <= i; j++) { dp[i][j] = 0; if (j != i)dp[i][j] = max(dp[i][j], dp[i - 1][j] + y * j * (t + (i - j - 1) * z)); if (j)dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + y * (j - 1) * (t + (i - j) * z)); ans = max(ans, dp[i][j] + (t + (i - j) * z) * (n - i) * (x + y * j));} } return ans;}int main() { int cas = 0; scanf("%d", &T); while (T--) {scanf("%I64d%I64d%I64d%I64d%I64d", &n, &x, &y, &z, &t);printf("Case #%d: %I64d\n", ++cas, solve()); } return 0;}