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 ...
随机推荐
- php 命名空间
命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只要避免命名重复就可以解决,最常见的一种做法是约定一个前缀. 例:项目中有两个 ...
- 安装TFS2008最终版(转载)
一.安装操作系统:windows server 2003 + Sp2具体步骤: 1.安装windows server 2003时选用工作组(默认为workgroup).由于在工作组环境中部署,因此使用 ...
- ASP.NET MVC 4 跨域
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1
<白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...
- LayoutComponent类,用于layout的组件类。 LayoutComponent保存的所有用于布局的数据。
LayoutComponent () 默认构造函数 更多... ~LayoutComponent () 默认的析构函数 更多... CREATE_FUNC (LayoutC ...
- 服务器部署之 cap deploy:setup
文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com $ cap deploy:setup 执行到这一步的时候会时间较长,可以直接中断 * executing &q ...
- 详解HttpURLConnection
请求响应流程 设置连接参数的方法 setAllowUserInteraction setDoInput setDoOutput setIfModifiedSince setUseCaches setD ...
- load url from future 解释
利用url 标签之后,不管urlpatterns里的某个地址叫法怎么改变,Templates里的地址都不用修改了.在模版中调用url标签的时候,需要:{% load url from future % ...
- C++复数四则运算的实现
程序主要实现复数的加减乘,数乘,取共轭功能. 将所有函数都定义为了成员函数. 使用库函数atof将字符串转换为浮点型数据. 函数主要难点在于处理输入.由于需要判断输入是选择退出还是继续,所以用字符串来 ...
- ListBox1控件
前台: <div> <asp:ListBox ID="ListBox1" runat="server" AutoPostBack=" ...