LA 4987 背包
题意:
有n个施工队,给定他们的位置,有m个防空洞,给定位置,求将施工队放到m个防空洞里面,最少的总距离?
n<=4000
分析:
dp[i][j] 前 i 个施工队,放到前 j 个防空洞里面的最少距离;
dp(i+1,j) = min(dp(i,j),dp(i,j-1)) + dist(a[i] - b[j]);
DP采用滚动数组;
那么,第二维的防空洞该怎么循环呢?
因为,每个防空洞都要有,那么这类似于背包中的容量,刷表的方式;
#include <bits/stdc++.h> using namespace std; const int maxn = +;
const int inf = 0x3f3f3f3f; struct node {
int d;
int id;
int ans;
}A[maxn],B[maxn]; long long dp[maxn];
int path[maxn][maxn];
int n,m; bool cmp(node a,node b) {
if(a.d==b.d)
return a.id < b.id;
return a.d < b.d;
} void find_path(int i,int j) {
if(i)
find_path(i-,path[i][j]);
A[i].ans = B[j].id;
} int cmp1(node a,node b) {
return a.id < b.id;
} int main()
{
while(~scanf("%d",&n)) {
for(int i=;i<n;i++)
{
scanf("%d",&A[i].d);
A[i].id = i;
} scanf("%d",&m);
for(int i=;i<m;i++) {
scanf("%d",&B[i].d);
B[i].id = i;
} sort(A,A+n,cmp);
sort(B,B+m,cmp); memset(dp,inf,sizeof(dp));
dp[] = abs(A[].d-B[].d); for(int i=;i<n;i++) {
for(int j=min(m-,i);j>=;j--) {
if(!j||dp[j]<dp[j-]) {
path[i][j] = j;
dp[j] = dp[j] + abs(A[i].d-B[j].d);
}
else {
path[i][j] = j-;
dp[j] = dp[j-] + abs(A[i].d - B[j].d);
}
}
} printf("%lld\n",dp[m-]);
find_path(n-,m-);
sort(A,A+n,cmp1); for(int i=;i<n;i++)
printf("%d ",A[i].ans+);
puts(""); }
return ;
}
LA 4987 背包的更多相关文章
- LA 4015 树形背包
题目链接:https://vjudge.net/contest/164840#problem/D 题意: 给一棵树,每条边上有一些权值,求 长度不超过 x ,最多能走多少个点: 分析: 考虑每一个节点 ...
- hdu 5188(带限制的01背包)
zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5188 zhx and contest(带限制条件的 01背包)
Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...
- BZOJ 4987 (树形DP)
###题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4987 ###分析 先考虑贪心,显然k个节点形成一棵树 求出树的直径,显然直径应该只被经 ...
- bzoj4987 Tree 树上背包
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4987 题解 一道还不错的题咯. 很容易发现一个结论:这 \(k\) 个点构成的一定是一个连通块 ...
- HDU 5234 背包。
J - 10 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 2249: Altruistic Amphibians 01背包
Description A set of frogs have accidentally fallen to the bottom of a large pit. Their only means o ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
随机推荐
- linux对于zombie的处理
@(Linux基础)[僵尸进程处理] 今天在服务器上推送项目的时候,突然发现很卡.就用top查看了一下,果然此事不简单啊. top - 10:39:16 up 20 days, 23:11, 2 us ...
- Oracle broker--详解
1,简介 01,介绍 Data Guard broker是建立在Data Guard基础上的一个对Data Guard配置,集中管理操作的一个平台.我们再上次DG主备切换的时候会发现特别麻烦,为此br ...
- zabbix 监控ipmi
一,配置ipmi yum -y install OpenIPMI OpenIPMI-devel ipmitool freeipmi 登入IDARAC 在服务端测试是否可以获得数据 ipmitool - ...
- TOJ 4002 Palindrome Generator
描述 A palindrome is a number that reads the same whether you read it from left to right or from right ...
- Eclipse设置JVM的内存参数
打开Eclipse 或者 MyEclipse 打开 Windows -> Preferences -> Java -> Installed JREs 选中你所使用的 JDK,然后点击 ...
- SEH结构
首先有几点问题 1.在后文中看到的PE的节中的配置信息表Load configuration是对SEH回调函数的注册,那么Exception Table是加载的什么信息. 2.什么时候走进系统异常,什 ...
- HDU 2795——Billboard——————【单点更新、求最小位置】
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- 记自己的hexo个人博客
https://othercoding.github.io/
- js.css嵌入dll
处理请求,返回 public ActionResult Get() { //传递一个部分名称 var n = Request["n"]; n = n.Replace('/', '. ...
- 码农的好助手:版本管理工具git的使用
一.什么是github? GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本库格式进行托管,故名 GitHub. GitHub 于 2008 年 4 月 10 日 ...