hdu 4362 Dragon Ball
来源:互联网 发布:淘宝网购达人贷 编辑:程序博客网 时间:2024/06/16 02:42
Dragon Ball
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2204 Accepted Submission(s): 770
Problem Description
Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball will disappear so he can only and must get one Dragon ball in each period.Digging out one ball he will lose some energy.Sean will lose |x-y| energy when he move from x to y.Suppose Sean has enough time to get any drogan ball he want in each period.We want to know the minimum energy sean will lose to get all period’s dragon ball.
Input
In the first line a number T indicate the number of test cases.Then for each case the first line contain 3 numbers m,n,x(1<=m<=50,1<=n<=1000),indicate m period Dragon ball will appear,n dragon balls for every period, x is the initial location of sean.Then two m*n matrix. For the first matrix,the number in I row and J column indicate the location of J-th Dragon ball in I th period.For the second matrix the number in I row and J column indicate the energy sean will lose for J-th Dragon ball in I-th period.
Output
For each case print a number means the minimum energy sean will lose.
Sample Input
13 2 52 34 11 31 11 34 2
Sample Output
8
Author
FZU
Source
2012 Multi-University Training Contest 7
题意:
m个周期,每个周期出现n个龙珠,刚开始在x位置,从x位置到y位置消耗的能量值是两者差的绝对值+在y位 置的龙珠要吸收的能量值,每个周期必须要在一个龙珠的位置。
题解:
很容易想到动态规划方程a[i][k].dp=min(a[i][k].dp,(a[i-1][j].dp+|a[i][k].x-a[i-1][j].x|+a[i][k].v)),这样直接用复 杂度为O(m*n*n)会超时,需要优化,把绝对值去掉,会出现两种情况
1.a[i][k].x>=a[i-1][j].x(前一个周期龙珠的位置小于后一个周期龙珠的位置)
这时a[i-1][j].dp-a[i-1][j].x+a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x 此时a[i][k].dp=mini+a[i][k].x+a[i] [k].v 对a[i][k].dp,a[i][k].x和a[i][k].v为固定的,在所有的a[i][k].x>=a[i-1][j].x中,只需找到最小的mini就可以 了,此时对所有j,只需遍历一遍j就可以了,不需要每个k都遍历一遍j了,因为对后一个k值,其i-1行x值小于现在 的x值的,只会多在前面的基础上多数,前面的最小的只需与后来多的比较就可以了。
2.a[i][k].x<=a[i-1][j].x(前一周期龙珠的位置大于后一个周期龙珠的位置)
这时a[i-1][j].dp+a[i-1][j].x-a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x,此时只需在所有的前一周期龙珠的 位置大于后一个周期龙珠的位置中找最小的mini就可以了。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int inf=1999999999;struct node{ int x; int v; int dp;}a[55][1100];int bbs(int x){ if(x<0) x=-x; return x;}bool cmp(node l,node r){ return l.x<r.x;}int main(){ int t,n,m,x; scanf("%d",&t); while(t--) { scanf("%d%d%d",&m,&n,&x); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { scanf("%d",&a[i][j].x); a[i][j].dp=inf; } } for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { scanf("%d",&a[i][j].v); } } sort(a[1]+1,a[1]+1+n,cmp); for(int i=1;i<=n;i++) a[1][i].dp=bbs(x-a[1][i].x)+a[1][i].v;//第1周期特殊处理 for(int i=2;i<=m;i++) { sort(a[i]+1,a[i]+1+n,cmp); //前一个周期龙珠的位置小于后一个周期龙珠的位置 int j=1; int mini=inf; for(int k=1;k<=n;k++) { while(j<=n&&a[i][k].x>=a[i-1][j].x) { mini=min(mini,(a[i-1][j].dp-a[i-1][j].x)); j++; } a[i][k].dp=min(a[i][k].dp,(mini+a[i][k].x+a[i][k].v)); } //前一周期龙珠的位置大于后一个周期龙珠的位置 j=n; mini=inf; for(int k=n;k>=1;k--) { while(j>=1&&a[i-1][j].x>=a[i][k].x) { mini=min(mini,(a[i-1][j].dp+a[i-1][j].x)); j--; } a[i][k].dp=min(a[i][k].dp,(mini-a[i][k].x+a[i][k].v)); } } int ans=inf; for(int i=1;i<=n;i++) { ans=min(ans,a[m][i].dp); } printf("%d\n",ans); } return 0;}
0 0
- HDU 4362 - Dragon Ball
- hdu 4362 Dragon Ball
- hdu 4362 Dragon Ball(dp)
- hdu 4362 Dragon Ball(优先队列+dp)
- HDU 4362 Dragon Ball 线段树
- HDU 3872 Dragon Ball
- hdu 3872 Dragon Ball
- HDU-4363-Dragon Ball
- hdu 4362 Dragon Ball 很裸的DP
- hdu 4362 Dragon Ball 单调队列优化 dp
- HDU 4362 Dragon Ball(维护最小值DP优化)
- HDU 3872 Dragon Ball(DP+线段树+单调栈)
- HDU_3872 Dragon Ball dp
- 【DP】 hdu4362 Dragon Ball
- Dragon Ball--hdoj
- HDU Ball
- Ball HDU
- hdu_4362 Dragon Ball (DP+单调栈)
- Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom
- 黄聪:解决python中文处理乱码,先要弄懂“字符”和“字节”的差别
- Java 给定一个日期,求对应的周数、星期几等
- SecureCRT 5.2.2的注册码
- iis中404问题
- hdu 4362 Dragon Ball
- 9.2.2、Libgdx的输入处理之事件处理
- nginx 配置HTTPS服务器
- 在Mvc中碰到从客户端中检测到有潜在危险的 Request.Form 值 的解决方法
- 黄聪:python控制台无法正常显示中文字符串解决方法
- 解决Android SDK Manager无法更新问题
- mysql数据库基础学习笔记
- Qt 5的git源码编译
- 黄聪:用python知道URL地址提取链接中的域名与端口