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的邮票,可以推出 ...
随机推荐
- How to download Heavy Duty Diagnostic Caterpillar SIS 2018 software
Maybe you find there are supplied Caterpillar SIS 2018 software free download in search engine, that ...
- my32_ error 1872 Slave failed to initialize relay log info structure from the repository
重启了实例后,slave进程无法开启 Last_SQL_Errno: Last_SQL_Error: Slave failed to initialize relay log info structu ...
- STM32F407使用MFRC522射频卡调试及程序移植成功
版权声明:转载请注明出处,谢谢 https://blog.csdn.net/Kevin_8_Lee/article/details/88865556 或 https://www.cnblogs.co ...
- C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密
要求:密码必须包含数字和字母 思路:1.列出数字和字符. 组成字符串 :chars 2.利用randrom.Next(int i)返回一个小于所指定最大值的非负随机数. 3. 随机取不小于chars长 ...
- javascript中for in与in的用法
1.For...In 声明用于对数组或者对象的属性进行循环/迭代操作. 对于数组 ,迭代出来的是数组元 素,对于对象 ,迭代出来的是对象的属性: var x var mycars = new Arra ...
- mysql存储过程嵌套循环并分页处理数据
业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新.版本发布前,通过当前存储过程解决数据升级问题. ##创建存证文档关联情况下更新所用存储过程 ...
- C#-01.语法基础
a. 语法基础 i. 命名空间(namespace):是 C# 中组织代码的方式,用来声明命名空间 . 语法:namespace 命名空间名称{ //命名空间的声明 } . 作用:可以把紧密相关的一些 ...
- javascript方法重载惹的祸
先贴出代码,看看执行结果会是什么? function ShowMsg() { //函数1 this.sure = function () { alert("ok"); }; //函 ...
- [JAVA小项目]GUI界面的局域网聊天室
思路: 1.服务端: 1.1 创建ServerSocket 监听本地端口 1.2 循环接收多个客户端的连接,并且把多个客户端连接的每个管道都为其创建线程. 服务端类的成员:链表--每个成员都是线程类- ...
- jq实现发送验证码倒计时60s
setInterval() :按照指定的周期(以毫秒计)来调用函数或计算表达式.方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭. setTimeout() :在指定的毫 ...