题意:

有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 背包的更多相关文章

  1. LA 4015 树形背包

    题目链接:https://vjudge.net/contest/164840#problem/D 题意: 给一棵树,每条边上有一些权值,求 长度不超过 x ,最多能走多少个点: 分析: 考虑每一个节点 ...

  2. hdu 5188(带限制的01背包)

    zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. hdu 5188 zhx and contest [ 排序 + 背包 ]

    传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  4. HDU 5188 zhx and contest(带限制条件的 01背包)

    Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...

  5. BZOJ 4987 (树形DP)

    ###题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4987 ###分析 先考虑贪心,显然k个节点形成一棵树 求出树的直径,显然直径应该只被经 ...

  6. bzoj4987 Tree 树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4987 题解 一道还不错的题咯. 很容易发现一个结论:这 \(k\) 个点构成的一定是一个连通块 ...

  7. HDU 5234 背包。

    J - 10 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 2249: Altruistic Amphibians 01背包

    Description A set of frogs have accidentally fallen to the bottom of a large pit. Their only means o ...

  9. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

随机推荐

  1. 转 C#对多个集合和数组的操作(合并,去重,判断)

    在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数. 首先举例2个集合A,B. List<int> ...

  2. js动态给textarea赋值

    document.getElementById("new_analysed_news").value=datas.weatherContent;

  3. oracle 基础知识(十二)----索引

    一, 索引介绍 索引与表一样,也属于段(segment)的一种.里面存放了用户的数据,跟表一样需要占用磁盘空间.索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是一个独立于 ...

  4. (转)linux sed命令就是这么简单

    sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...

  5. 内置组件 && vue中强大的缓存机制之keep-alive

    vue中强大的缓存机制之keep-alive 最近在用vue做项目,在切换页面时发现切换回原来的页面无法保存原来的状态. 如A页面需要ajax请求数据,然后切换到B页面做某些事情,再切换回A页面时,A ...

  6. 如何在DIV内只要垂直滚动条,不要水平滚动条

    <DIV style="OVERFLOW-Y: scroll; OVERFLOW-X:hidden; width: 685px; height: 180px">< ...

  7. 【Ubuntu】ubuntu 16.04 设置root用户初始密码

    安装ubuntu成功后,都是普通用户权限,并没有最高root权限,如果需要使用root权限的时候,通常都会在命令前面加上 sudo . 我们一般使用su命令来直接切换到root用户的,但是如果没有给r ...

  8. [转]谷歌Chrome浏览器开发者工具教程—基础功能篇

    来源:http://www.xiazaiba.com/jiaocheng/5557.html Chrome(F12开发者工具)是非常实用的开发辅助工具,对于前端开发者简直就是神器,但苦于开发者工具是英 ...

  9. Investigating issues with Unmanaged Memory. First steps. (转载)

    原文:http://kate-butenko.blogspot.tw/2012/07/investigating-issues-with-unmanaged.html I want to write ...

  10. ControlTemplate 中 Bingding 附加属性时需要加入 Path

    <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource MaterialDesignGroup ...