Dire Wolf

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 3815    Accepted Submission(s): 2266

Problem 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 bi. 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 bi 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 ai (0 ≤ ai ≤ 100000), denoting the basic attack of each dire wolf.

The third line contains N integers bi (0 ≤ bi ≤ 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.

 

题目大意:你是一个战士现在面对,一群狼,每只狼都有一定的主动攻击力和附带攻击力。你杀死一只狼。你会受到这只狼的(主动攻击力+旁边两只狼的附带攻击力)这么多伤害~现在问你如何选择杀狼的顺序使的杀完所有狼时,自己受到的伤害最小。(提醒,狼杀死后就消失,身边原本相隔的两只狼会变成相邻,而且不需要考虑狼围城环这种情况)

输入要求:总共T组数据,每组N只狼,按顺序输入全部狼的主动攻击力和然后再按顺序输入全部狼的附带攻击力

输出要求:Case #x: y x第几组数据,y最少受到的伤害

样例解释: 
2

输入 

3 5 7 
8 2 0 
输出 
Case #1: 17 
杀序号1的狼 3+2=5 
杀序号2的狼 5+0=5 
杀序号3的狼 7+0=7 
总的伤害5+5+7=17;

10 
1 3 5 7 9 2 4 6 8 10 
9 4 1 2 1 2 1 4 5 1 
Case #2: 74 
主动伤害总和1+3+5+7+9+2+4+6+8+10=55; 
附带伤害总和 
因为数字比较多我这次列出全部过程 
【1】3 5 7 9 2 4 6 8 10 
【9】 4 1 2 1 2 1 4 5 1 
附带伤害 4 
【1 3】 5 7 9 2 4 6 8 10 
【9 4】 1 2 1 2 1 4 5 1 
附带伤害 1 
【1 3 】5 【7】 9 2 4 6 8 10 
【9 4】 1 【2】 1 2 1 4 5 1 
附带伤害 1+1=2 
【1 3】 5【 7】 9 【2】 4 6 8 10 
【9 4】 1 【2】 1 【2】 1 4 5 1 
附带伤害 1+1=2; 
此时实际序列5 9 4 6 8 10 
1 1 1 4 5 1 
【1 3】 5【 7】 9 【2】 4 6【 8】 10 
【9 4】 1 【2】 1 【2】 1 4 【5】 1 
附带伤害 4+1=5 
【1 3】 5【 7】 9 【2】 4【 6 8】 10 
【9 4】 1 【2】 1 【2】 1 【4 5】1 
附带伤害 1+1=2 
剩下4只狼 
5 9 4 10 
1 1 1 1 
从左边杀起,因这样每次只有右边有附带伤害 
1+1+1=3 
附带伤害总和4+1+2+2+5+2+3=19 
总和伤害55+19=74

思路:区间dp,用贪心竟然wa了,肯定哪里有错了,呜呜呜

区间dp的伪代码

//mst(dp,0) 初始化DP数组
for(int i=;i<=n;i++)
{
dp[i][i]=初始值
}
for(int len=;len<=n;len++) //区间长度
for(int i=;i<=n;i++) //枚举起点
{
int j=i+len-; //区间终点
if(j>n) break; //越界结束
for(int k=i;k<j;k++) //枚举分割点,构造状态转移方程
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]+w[i][j]);
}
}

题目AC代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define ll long long
int const maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int attack[];
int extre[];
int dp[][]; //第i到第j个的最小受到伤害 int main()
{
int t;
cin>>t;
int ca=;
while(t--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>attack[i];
for(int i=;i<=n;i++)
cin>>extre[i];
extre[]=; //初始边界情况
extre[n+]=;
for(int i=;i<=n;i++)
//刚开始我写成memset(dp,INF,sizeof(dp));这样是错的因为下面有dp[i][k-1]+dp[k+1][j]在每次循环时第一遍因为j==k,所以k+1>j
 //  如果按memset初始化,dp[k+1][j]和[dp[i][j]都是超级大的数,min就会出错。
 
for(int j=i;j<=n;j++) dp[i][j]=INF; for(int i=;i<=n;i++) 
{
dp[][i]=;
dp[i][n+]=;
}
for(int len=;len<=n;len++)
for(int i=;i<=n;i++)
{ int j=i+len-;
if(j>n)
break;
for(int k=i;k<=j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k-]+dp[k+][j]+attack[k]+extre[i-]+extre[j+]);
}
}
printf("Case #%d: %d\n",ca++,dp[][n]);
}
}

Dire Wolf HDU - 5115(区间dp)的更多相关文章

  1. Dire Wolf——HDU5115(区间DP)

    题意 就是有一对狼,每个狼有初始的攻击力,并且还能给左右两边的狼提供攻击力加成,当冒险家杀死一头狼的时候他也会受到这个狼目前攻击力的伤害 实例解析 33 5 78 2 0 有三头狼,刚开始第二头狼给他 ...

  2. Dire Wolf HDU - 5115

    题目链接 一开始很自然的想到了贪心,跑了一下贪心,发现无法处理某一段已经被选走的情况,根据数据范围,区间dp比较适合,能储存区间取样信息 设dp[i][j]为已经杀死区间[i,j]的最小值,可以得到转 ...

  3. Dire Wolf ---hdu5115(区间dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题意:有一排狼,每只狼有一个伤害A,还有一个伤害B.杀死一只狼的时候,会受到这只狼的伤害A和这只 ...

  4. hdu 5115 区间dp ***

    题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么我们受到的伤害值为这只狼的攻击值与它旁边的两只狼的附加值的和,求把所有狼都杀光受到的最小的伤害值. 枚举中间k作为最后杀死的 ...

  5. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  7. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  8. String painter HDU - 2476 -区间DP

    HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...

  9. HDU 4632 区间DP 取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...

随机推荐

  1. android 开发-数据存储之共享参数

    android提供5中数据存储方式 数据存储之共享参数 内部存储 扩展存储 数据库存储 网络存储  而共享存储提供一种可以让用户存储保存一些持久化键值对在文件中,以供其他应用对这些共享参数进行调用.共 ...

  2. Git远程推送文件太大的error解决

    error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errfno 10054 方法1: 改成ssh推送 方法2: 把推送的缓 ...

  3. ElasticSearch安装(以Docker的方式)

    拉取docker镜像: docker pull docker.elastic.co/elasticsearch/elasticsearch:6.1.1 命令行方式启动 测试环境     :docker ...

  4. 移植mavlink协议到STM32详细教程

    1准备材料, 首先准备一个带串口的stm32程序(这里选用整点原子的官方串口例程这里自己去找不讲)作者:恒久力行 QQ:624668529,然后去mavlink官网下载mavlink源码,这里重点讲解 ...

  5. 日常bug整理--xxtz

    2017-12-12 建SQLite数据库表时,遇到外键关联报错:foreign key mismatch 解决:发现是个粗心问题,关联的外键没有作为主键,原因是关联的外键由INT改为varchar字 ...

  6. idea 清屏(控制台)快捷键

    eclipse清屏快捷键为鼠标右键+R 而在idea中默认并没有清屏console的快捷键 所以需要我们自行设置: 1,ctrl+alt+s打开settings 2,找到keymap 3,搜索 cle ...

  7. python之re模块和正则表达式

    今天我们来谈谈python中模块的使用,在探讨模块前先来了解一下正则表达式的具体用法. 1.正则表达式 正则表达式就是匹配 字符串内容的一种规则.谈到正则就和字符串相关了,首先我们要知道什么是字符组. ...

  8. 响应式及Bootstrap

    一丶CSS3的@media 查询 使用 @media 查询,你可以针对不同的屏幕大小定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@med ...

  9. iis6.0 建立站点

    公司网站的服务器甚多 其中还包括早期的iis6.0 的网站服务器 由于之前没有接触过 特此记录建立站点过程和注意事项 1.每个站点最好都新建一个用户名 方便管理 这里操作系统是 winXP 现在运行界 ...

  10. OpenCV之cvAddWeighted直接C语言实现版addWeighted,应对上下平滑融合拼接

    关于OpenCV中的cvAddWeighted的介绍可参见<opencv中的cvAddWeighted函数> cvAddWeighted有个问题,它只能实现两张图片的直接融合,往往产生明显 ...