UVALive 4987---Evacuation Plan(区间DP)
题目链接
problem Description
Flatland government is building a new highway that will be used to transport weapons from its main weapon plant to the frontline in order to support the undergoing military operation against its neighbor country Edgeland. Highway is a straight line and there are n construction teams working at some points on it. During last days the threat of a nuclear attack from Edgeland has significantly increased. Therefore the construction office has decided to develop an evacuation plan for the construction teams in case of a nuclear attack. There are m shelters located near the constructed highway. This evacuation plan must assign each team to a shelter that it should use in case of an attack. Each shelter entrance must be securely locked from the inside to prevent any damage to the shelter itself. So, for each shelter there must be some team that goes to this shelter in case of an attack. The office must also supply fuel to each team, so that it can drive to its assigned shelter in case of an attack. The amount of fuel that is needed is proportional to the distance from the team’s location to the assigned shelter. To minimize evacuation costs, the office would like to create a plan that minimizes the total fuel needed. Your task is to help them develop such a plan.
Input
The input file contains several test cases, each of them as described below. The first line of the input file contains n — the number of construction teams (1 ≤ n ≤ 4000). The second line contains n integer numbers - the locations of the teams. Each team’s location is a positive integer not exceeding 109 , all team locations are different. The third line of the input file contains m — the number of shelters (1 ≤ m ≤ n). The fourth line contains m integer numbers — the locations of the shelters. Each shelter’s location is a positive integer not exceeding 109 , all shelter locations are different. The amount of fuel that needs to be supplied to a team at location x that goes to a shelter at location y is equal to |x − y|.
Output
For each test case, the output must follow the description below. The first line of the output file must contain z — the total amount of fuel needed. The second line must contain n integer numbers: for each team output the number of the shelter that it should be assigned to. Shelters are numbered from 1 to m in the order they are listed in the input file.
Sample Input
3
1 2 3
2
2 10
Sample Output
8
1 1 2
题意:输入n 然后输入n个施工队的位置(一维坐标) 然后输入m 再输入m个防御点的位置(一维坐标),1<=m<=n<=4000 一维坐标小于1e9 现在让所有的施工队进入防御点,且每个防御点必须有施工队进入,求所有施工队走的最小距离和,并输出每个施工队去的防御点编号;
思路:区间DP,定义dp[i][j] 表示前i个施工队进入j个防御点的最小距离和,那么有状态转移方程:dp[i][j]=max{dp[i-1][j-1],dp[i-1][j]}+abs(a[i]-b[j]) 注意要先对输入的施工队和防御点进行从小到大的排序;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m;
long long dp[][];
bool vis[][]; struct Node
{
long long x;
int id;
int t;
bool operator < (const Node & tt) const
{ return x < tt.x; }
}a[],b[]; bool cmp(const Node s1,const Node s2)
{
return s1.id<s2.id;
} void print(int x,int y)
{
if(y==&&x==){
a[x].t=b[y].id;
return ;
}
print(x-,y-+vis[x][y]);
a[x].t=b[y].id;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i].x);
a[i].id=i;
}
sort(a+,a+n+);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%lld",&b[i].x);
b[i].id=i;
}
sort(b+,b+m+); memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=m&&j<=i;j++)
{
if(j==)
{
dp[i][j]=dp[i-][j]+abs(a[i].x-b[j].x);
vis[i][j]=true;
}
else if(j==i)
{
dp[i][j]=dp[i-][j-]+abs(a[i].x-b[j].x);
vis[i][j]=false;
}
else
{
dp[i][j]=min(dp[i-][j],dp[i-][j-])+abs(a[i].x-b[j].x);
vis[i][j]=(dp[i-][j]>dp[i-][j-])?false:true;
}
}
}
cout<<dp[n][m]<<endl;
print(n,m);
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
printf("%d%c",a[i].t,(i==n)?'\n':' ');
}
return ;
}
UVALive 4987---Evacuation Plan(区间DP)的更多相关文章
- HDU 3757 Evacuation Plan DP
跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...
- uvalive 6938 区间dp
看到n范围和给的区间看着就像区间dp 然后怎么cmp感觉都没法进行区间合并 n的300误导了下 没有注意离散化之后对时间可以dp 然而这个dp感觉不太经得起证明的样子... dp[i][j] -> ...
- hdu 4412 Sky Soldiers(区间DP)
Sky Soldiers Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- POJ2175 Evacuation Plan
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4617 Accepted: 1218 ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- 爱上MVC~一个Action多套View模版的设计
回到目录 模块化 这个问题是在做模块化设计时出现的,在Lind.DDD.Plugins模块里,需要对应的模块实体,模块管理者,模块标识接口等,开发时,如果你的功能点属于一个模块,需要实现IPlugin ...
- weinre使用
2016-1-21 更新说明: 微信web开发者工具已经集成了weinre,只需设置手机代理便可调试任意页面,更简单更方便,推荐使用! Web应用开发者需要针对手机进行界面的调试,但是手机上并没有称心 ...
- “胡”说IC——菜鸟工程师完美进阶
“胡”说IC——菜鸟工程师完美进阶(数十位行业精英故事分享,顶级猎头十多年来经验总结,对将入或初入IC电子业“菜鸟”职业发展.规划的解惑和点拨.) 胡运旺 编著 ISBN 978-7-121-22 ...
- 更新日志 - fir.im「高级统计」功能上线
距离 2016 年到来只剩 10 个日夜,fir.im 也准备了一些新鲜的东西,比如「高级统计」功能和「跳转应用商店」功能,帮助你更好地管理.优化应用,欢迎大家试用反馈:) 新增高级统计功能 这次更新 ...
- WPF入门教程系列七——布局之WrapPanel与StackPanel(二)
三. WrapPanel WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够是就会自动调整进行换行,后续排序按照从上至下或从右至左的顺序进行. Orientation— ...
- How Google TestsSoftware - Part One
This is the firstin a series of posts on this topic.The one question I get morethan any other is &qu ...
- 快速入门系列--WCF--08扩展与新特性
最后一章将进行WCF扩展和新特性的学习,这部分内容有一定深度,有一个基本的了解即可,当需要自定义一个完整的SOA框架时,可以再进行细致的学习和实践. 服务端架构体系的构建主要包含接下来的几个要素:服务 ...
- 免费在线loading生成。
loading这个在项目中也是经常要使用,这里推荐一个网站http://www.ajaxload.info/可以在线生成loading. 进来页面是这样的. 勾选transparent将会生成透明的g ...
- Render OpenCascade Geometry Surfaces in OpenSceneGraph
在OpenSceneGraph中绘制OpenCascade的曲面 Render OpenCascade Geometry Surfaces in OpenSceneGraph eryar@163.co ...
- [c++] STL = Standard Template Library
How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...