解题报告 之 hdu5115 Dire Wolf
Description
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor.
Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful beasts, 8 - 9 feet long and weighing 600 - 800 pounds, are the most well-known orc mounts. As tall as a man, these great wolves have long tusked jaws that look like they could snap an iron bar. They have burning red eyes. Dire wolves are mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.
Dire wolves are efficient pack hunters that kill anything they catch. They prefer to attack in packs, surrounding and flanking a foe when they can.
― Wowpedia, Your wiki guide to the World of Warcra�
Matt, an adventurer from the Eastern Kingdoms, meets a pack of dire wolves. There are N wolves standing in a row (numbered with 1 to N from left to right). Matt has to defeat all of them to survive.
Once Matt defeats a dire wolf, he will take some damage which is equal to the wolf’s current attack. As gregarious beasts, each dire wolf i can increase its adjacent wolves’ attack by b i. Thus, each dire wolf i’s current attack consists of two parts, its basic attack ai and the extra attack provided by the current adjacent wolves. The increase of attack is temporary. Once a wolf is defeated, its adjacent wolves will no longer get extra attack from it. However, these two wolves (if exist) will become adjacent to each other now.
For example, suppose there are 3 dire wolves standing in a row, whose basic attacks ai are (3, 5, 7), respectively. The extra attacks b i they can provide are (8, 2, 0). Thus, the current attacks of them are (5, 13, 9). If Matt defeats the second wolf first, he will get 13 points of damage and the alive wolves’ current attacks become (3, 15).
As an alert and resourceful adventurer, Matt can decide the order of the dire wolves he defeats. Therefore, he wants to know the least damage he has to take to defeat all the wolves.
Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful beasts, 8 - 9 feet long and weighing 600 - 800 pounds, are the most well-known orc mounts. As tall as a man, these great wolves have long tusked jaws that look like they could snap an iron bar. They have burning red eyes. Dire wolves are mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.
Dire wolves are efficient pack hunters that kill anything they catch. They prefer to attack in packs, surrounding and flanking a foe when they can.
― Wowpedia, Your wiki guide to the World of Warcra�
Matt, an adventurer from the Eastern Kingdoms, meets a pack of dire wolves. There are N wolves standing in a row (numbered with 1 to N from left to right). Matt has to defeat all of them to survive.
Once Matt defeats a dire wolf, he will take some damage which is equal to the wolf’s current attack. As gregarious beasts, each dire wolf i can increase its adjacent wolves’ attack by b i. Thus, each dire wolf i’s current attack consists of two parts, its basic attack ai and the extra attack provided by the current adjacent wolves. The increase of attack is temporary. Once a wolf is defeated, its adjacent wolves will no longer get extra attack from it. However, these two wolves (if exist) will become adjacent to each other now.
For example, suppose there are 3 dire wolves standing in a row, whose basic attacks ai are (3, 5, 7), respectively. The extra attacks b i they can provide are (8, 2, 0). Thus, the current attacks of them are (5, 13, 9). If Matt defeats the second wolf first, he will get 13 points of damage and the alive wolves’ current attacks become (3, 15).
As an alert and resourceful adventurer, Matt can decide the order of the dire wolves he defeats. Therefore, he wants to know the least damage he has to take to defeat all the wolves.
Input
The first line contains only one integer T , which indicates the number of test cases. For each test case, the first line contains only one integer N (2 ≤ N ≤ 200).
The second line contains N integers a i (0 ≤ a i ≤ 100000), denoting the basic attack of each dire wolf.
The third line contains N integers b i (0 ≤ b i ≤ 50000), denoting the extra attack each dire wolf can provide.
The second line contains N integers a i (0 ≤ a i ≤ 100000), denoting the basic attack of each dire wolf.
The third line contains N integers b i (0 ≤ b i ≤ 50000), denoting the extra attack each dire wolf can provide.
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1), y is the least damage Matt needs to take.
Sample Input
2 3 3 5 7 8 2 0 10 1 3 5 7 9 2 4 6 8 10 9 4 1 2 1 2 1 4 5 1
Sample Output
Case #1: 17 Case #2: 74
Hint
In the first sample, Matt defeats the dire wolves from left to right. He takes 5 + 5 + 7 = 17 points of damage which is the least damage he has to take.
</pre></div><div><span style="font-family:'Microsoft YaHei'"><span style="font-size:32px"><span style="color:rgb(85,85,85); font-family:'Microsoft YaHei'; font-size:18px; line-height:35px">题目大意:n头狼,分别有一个攻击力,而且分别有一个加持力(来自大大神翻译),可以给左右两边的狼加相应的攻击力(边界的狼就只能给相邻的一头狼加),一个人要杀狼,杀一头狼时受到该狼的攻击力和来自周围的加持力。被杀死的狼的加持力消失,但其他狼又会更新加持力(因为相邻的位置变化了)。问该人能够把全部狼杀光,且最小受到多少伤害?</span></span></span></div><div><span style="font-family:'Microsoft YaHei'"><span style="font-size:32px"><span style="color:rgb(85,85,85); font-family:'Microsoft YaHei'; font-size:18px; line-height:35px"></span></span></span></div><div><span style="font-family:Microsoft YaHei; font-size:18px; color:#555555"><span style="line-height:35px">分析:一看就是DP。首先要明确没头狼固有的杀伤力是无意义的,直接全部相加到ans,作为答案的一部分即可。现在来考虑附加伤害add[]。用dp[l][r]表示将[l,r]头狼全部杀光的代价。然后状态转移,更新dp[l][r]时,考虑到dp[l][r]可能来自于将杀死了[l][r]中任意一头狼二产生的,那么我们就用k遍历[l,r],假设是刚刚杀死了第k头狼而使得[l,r]头狼全部被杀死。So,dp[l][r]=min(dp[l][k-1]+dp[k+1][r]+newDamage),但是注意k取l的时候没有前半部分,k取r的时候没有后半部分,注意处理一下即可。那么这个newDamage怎么算呢?注意到现在第k头狼的左边是第l-1头狼(可能不存在),而右边是r+1头狼(也可能不存在)。那么newDamage=add[l-1]+add[r+1]。这样我们最终的答案就是ans+dp[1][n]。输出即可。</span></span></div><div><span style="font-family:Microsoft YaHei; font-size:18px; color:#555555"><span style="line-height:35px"> DP可以采用记忆化搜索,我尝试了一下递推发现不好写那个循环,因为是按照长度递增的。所以还是用递归来写的。</span></span></div><div><span style="font-family:Microsoft YaHei; font-size:18px; color:#555555"><span style="line-height:35px"></span></span></div><div><span style="font-family:Microsoft YaHei; font-size:18px; color:#555555"><span style="line-height:35px"></span></span><pre name="code" class="cpp">#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int dp[220][220];
int add[220];
int DP(int l, int r)
{
if (l == r)
return add[l - 1] + add[r + 1];
if (dp[l][r] !=-1) return dp[l][r];
int MIN = 0x3f3f3f3f;
for (int k = l; k <= r; k++)
{
int tem = 0;
if (k > l) tem += DP(l, k - 1);
if (k < r)tem += DP(k + 1, r);
MIN = min(MIN, tem);
}
return dp[l][r]=MIN + add[l - 1]+add[r + 1];
}
int main()
{
int kase;
cin >> kase;
for (int step = 1; step <= kase; step++)
{
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++)
{
int tem;
cin >> tem;
ans += tem;
}
memset(add, 0, sizeof add);
memset(dp, -1, sizeof dp);
for (int i = 1; i <= n; i++)
{
cin >> add[i];
}
/*
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
cout << dp[i][j]<<" ";
cout << endl;
}*/
cout << "Case " << "#" << step << ": ";
cout << ans + DP(1,n) << endl;
}
}
最后要感叹一下当时区域赛没有做出这道题,我觉得这道题的难点在于dp的两个维度的变量是同一类型的值,即对等的遍历,一般我在想dp的时候都会考虑两个维度的量(比如时间和地点)。