HDU_5115_记忆化搜索

还是自己不够强大!这一切是自己选的,不管负责任还是不负责任。选择寂寞是因为自己想要变得更优秀,那就要有一颗与之相对的强大的内心!


题意:

攻击一排敌人,每个敌人有一个基础攻击力a,并提供给左右两个敌人一个附加攻击力b,没打倒一个敌人,就会受到被打倒敌人攻击力(包括基础的和旁边的敌人给的附加的)的伤害,问打倒所有敌人最少受到多少伤害。



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.
 

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.


比赛的时候我觉得这题是贪心,队友觉得是dp,然后我贪心的代码WA了,他推完说dp不可能。。。

结果就是DP,不过方向不好确定,用记忆化搜索实现。

dp[l][r]表示打倒l到r区间受到的最小损伤,转移方程dp[l][r]=min(dp[l][i-1]+dp[i+1][r])+a[i]+b[l-1]+b[r+1],

枚举最后一个打倒的敌人,第i位是最后一个打倒的敌人,我们先递归求解打倒两个区间的最小开销,然后剩下第i个位置的敌人,打倒他只需要a[i]+b[l-1]+b[r+1]。

不得不说这个dp好巧妙,枚举的不是下一个打倒的人,而是最后一个打倒的人,这个dp难就难在讨论打倒一个人之后,整个串都变了,dp的后续性不存在了,所以我们枚举打倒最后一个人,这样后续性就成立了。事实上,一个区间内某些人之间打倒的顺序变化无意义,但最后一个打倒就显然有意义,因为最后一个是唯一的。


代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 210
#define inf 0x3f3f3f3f
int a[mxn],n;
int dp[mxn][mxn];
int dfs(int l,int r){
	if(dp[l][r]!=inf)	return dp[l][r];
	if(l==r)	return a[l-1]+a[r+1];
	dp[l][r]=min(dfs(l+1,r),dfs(l,r-1))+a[l-1]+a[r+1];
	for(int i=l+1;i<r;++i)
		dp[l][r]=min(dp[l][r],dfs(l,i-1)+dfs(i+1,r)+a[l-1]+a[r+1]);
	return dp[l][r];
}
int main(){
	int cs,CS=0;
	scanf("%d",&cs);
	while(cs--){
		memset(dp,0x3f,sizeof(dp));
		int ans=0;
		scanf("%d",&n);
		int tem;
		for(int i=0;i<n;++i){
			scanf("%d",&tem);
			ans+=tem;
		}
		for(int i=1;i<=n;++i)	scanf("%d",&a[i]);
		a[0]=a[n+1]=0;
		ans+=dfs(1,n);
		printf("Case #%d: %d\n",++CS,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值