HDU 4362 Dragon Ball 贪心DP
Dragon Ball
题意:
给你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的更多相关文章
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...
- HDU 5903 Square Distance (贪心+DP)
题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...
- HDU 1051 Wooden Sticks 贪心||DP
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【BZOJ-3174】拯救小矮人 贪心 + DP
3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 686 Solved: 357[Submit][Status ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- 使用苏飞httphelper开发自动更新发布文章程序
最近新上线了一个网站,专门收集网上签到赚钱,有奖活动等等的网站 我就要集分宝 http://www.591jfb.com.新建立 了一个栏目"每日更新",这样就需要每天都登录到网站 ...
- 一机双mysql的安装和启动注意事项目
./configure --prefix=/usr/local/mysql5.1/ --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql5.1/ ...
- Silverlight实例教程 – Datagrid,Dataform数据验证和ValidationSummary(转载)
Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...
- Ioc注解
注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- C# 检测操作系统是否空闲,实现系统空闲后做一些操作
public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// </summary> [St ...
- ios框架中UIResponder的职责链设计模式应用
今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html
- 转:Java NIO系列教程(二) Channel
Java NIO的通道类似流,但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先读到一个Buffer,或者总是要从一个Bu ...
- 操作sqlserver数据库常用的三个方法
1. ADO.NET -> 连接字符串,常用的两种方式: server=计算机名或ip\实例名;database=数据库名;uid=sa;pwd=密码; server=计算机名或ip\实例名;d ...
- Javascript网页摇一摇
function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 window.addEventListener('devicemot ...
- kindle paperwhite折腾记
在亚马逊官网上买了一个kindle paperwhite 一代(849元) , 打算再买个皮套, 淘宝店 http://detail.tmall.com/item.htm?spm=a230r.1.1 ...