Dragon Ball

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
 
1 3 2 5 2 3 4 1 1 3 1 1 1 3 4 2
 
Sample Output
 
8
 

题意:

  给你m天,每天在任意的一位坐标轴上出现n个球pos[i][j],0时间的时候在x位置,

  从x走向y花费abs(x-y)的价值,拿掉这个球花费cost[i][j]

  问你每次时间你都必须走向一个球拿掉它,m天后 最小花费是多少

题解:

  设定dp[i][j]表示第i天后在第j个球的最小花费,

  容易想到这是一个n*m*n的转移

  给了1.5s,值得一试

  不过你把转移方程写出来:

      对于从当前位置左边转移过来的 dp[i][j] = dp[i-1][k] - pos[i-1][k] + pos[i][j] + cost[i][j];

    对于从当前位置右边转移过来的  dp[i][j] = dp[i-1][k] + pos[i-1][k]  -pos[i][j] + cos[i][j];

  其中dp[i-1][k],pos[i-1][k];都是上一层的,这个我们预处理出就好了啊

  即使 dp[i-1][k] - pos[i-1][k] 维护最小  dp[i-1][k] + pos[i-1][k]维护最小,再二分取两者最小就可以了

  求个前缀的事。。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1e3+, M = 1e4, mod = ,inf = 1e9;
typedef long long ll; int dp[][N],cost[][N],pos[][N],n,m,x,allpos[N],l[N],r[N];
pair<int,int > P[N];
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d%d%d",&m,&n,&x);
for(int i=;i<=m;i++)
for(int j=;j<=n;j++) scanf("%d",&pos[i][j]);
for(int i=;i<=m;i++)
for(int j=;j<=n;j++) scanf("%d",&cost[i][j]); for(int i=;i<=n;i++) dp[][i] = abs(x-pos[][i])+cost[][i]; for(int j=;j<=n;j++)
P[j] = make_pair(pos[][j],dp[][j] - pos[][j]);
sort(P+,P+n+);
for(int j=;j<=n;j++) allpos[j] = P[j].first; l[] = inf;
r[n+] = inf;
for(int j=;j<=n;j++)
l[j] = min(l[j-],P[j].second);
for(int j=;j<=n;j++)
P[j] = make_pair(pos[][j],dp[][j] + pos[][j]);
sort(P+,P+n+);
for(int j=n;j>=;j--)
r[j] = min(r[j+],P[j].second); for(int i=;i<=m;i++) { for(int j=;j<=n;j++) {
int tmp = upper_bound(allpos+,allpos+n+,pos[i][j])- allpos - ;
dp[i][j] = min(l[tmp] + cost[i][j]+pos[i][j],r[tmp+] + cost[i][j] - pos[i][j]);
// cout<<dp[i][j]<<" ";
}
for(int j=;j<=n;j++)
P[j] = make_pair(pos[i][j],dp[i][j] - pos[i][j]);
sort(P+,P+n+);
for(int j=;j<=n;j++) allpos[j] = P[j].first;
l[] = inf;
r[n+] = inf;
for(int j=;j<=n;j++)
l[j] = min(l[j-],P[j].second);
for(int j=;j<=n;j++)
P[j] = make_pair(pos[i][j],dp[i][j] + pos[i][j]);
sort(P+,P+n+);
for(int j=n;j>=;j--)
r[j] = min(r[j+],P[j].second);
// cout<<endl;
}
int ans = inf;
for(int i=;i<=n;i++) ans = min(dp[m][i],ans);
printf("%d\n",ans);
}
return ;
}

HDU 4362 Dragon Ball 贪心DP的更多相关文章

  1. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

  2. HDU 5903 Square Distance (贪心+DP)

    题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...

  3. HDU 1051 Wooden Sticks 贪心||DP

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  7. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  9. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. Linux 下安装配置 JDK7

    Linux 下安装配置 JDK7 配置环境(debian 7) 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将“Operating System Distributor Li ...

  2. html中table的画法及table和div的区别

    最近项目中,根据客户的要求需要在页面上展示各种报表什么的,各种表格的都会出现.这里也将table的画法,做一下总结.办法虽笨但很实用.这也是从高人那里学来的,总之是屡试不爽啊.就以下面的表格为例. 若 ...

  3. C++中虚函数的作用浅析

    虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 下面是对C++的虚函数这玩意儿的理解. 一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你 ...

  4. PHP访问数据,增删改

    主页面 <h1>主页面</h1> <table width="100%" border="1" cellpadding=" ...

  5. "微信全球商业创新大赛-创意中国2015"国际MBA商业挑战赛开启

    微信商业化进程一直在摸索中前进,从未间断.近日由中欧国际工商学院与腾讯(Tencent)共同发起举办“微信全球商业创新大赛 - 创意中国2015”(We Win with WeChat - INNOV ...

  6. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

  7. cpu和内存的关系

    CPU是负责运算和处理的,内存是交换数据的.当程序或者操作者对CPU发出指令,这些指令和数据暂存在内存里,在CPU空闲时传送给CPU,CPU处理后把结果输出到输出设备上,输出设备就是显示器,打印机等. ...

  8. Objective-C和其他C指针的转换

    首先看一下典型的NSString与CFStringRef的相互转换   http://www.tuicool.com/articles/MJRr226 // CFStringRef to NSStri ...

  9. 为win7添加ubuntu的启动引导项

    利用MBRFix删除ubuntu的开机引导界面,恢复成win7引导之后,为win7添加ubuntu的启动引导项: 直接利用EasyBCD添加一个Grub2的引导项即可 参考:http://mathis ...

  10. Install ssdb-rocks on CentOS 6

    Install ssdb-rocks on CentOS 6 C.C.  发表于 2014年08月10日 20:14 | Hits: 649 为了优化节操精选的弹幕系统,打算更换到Facebook的R ...