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 ...
随机推荐
- Atitit 判断判断一张图片是否包含另一张小图片
Atitit 判断判断一张图片是否包含另一张小图片 1. keyword1 2. 模板匹配是在图像中寻找目标的方法之一(切割+图像相似度计算)1 3. 匹配效果2 4. 图片相似度的算法(感知哈希算 ...
- react7 react 三目运算
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- 转【】浅谈sql中的in与not in,exists与not exists的区别_
浅谈sql中的in与not in,exists与not exists的区别 1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...
- react-native —— 在Windows下搭建React Native Android开发环境
在Windows下搭建React Native Android开发环境 前段时间在开发者头条收藏了 @天地之灵_邓鋆 分享的<在Windows下搭建React Native Android开发环 ...
- xdotool模拟击键和鼠标移动
最近双十一抢红包的活动比较火,我也就去玩了一下,在一个小活动里,需要不停的点击左箭头和右箭头,让红包不停的跑,但自己点的比较慢,老是出现下面的图片 看到提示还有n多公里才跑完,感觉极度不爽,一怒之下, ...
- Deep learning:五十(Deconvolution Network简单理解)
深度网络结构是由多个单层网络叠加而成的,而常见的单层网络按照编码解码情况可以分为下面3类: 既有encoder部分也有decoder部分:比如常见的RBM系列(由RBM可构成的DBM, DBN等),a ...
- Linux的IO性能监控
一般使用iostat命令监控I/O性能1.iostat命令可用参数列表: OPTIONS -c Display the CPU utilization report. -d Display the d ...
- EasyUI笔记
以下功能实现朋友们若有更好的思路办法,欢迎留言交流. 1.关闭其他标签页(右键菜单触发,保留左侧第一个欢迎tab) 问题:取到的数组tabs长度会随着tab的关闭而变化 思路:先遍历需要关闭的标签页t ...
- JDK8 的 Lambda 表达式原理
JDK8 使用一行 Lambda 表达式可以代替先前用匿名类五六行代码所做的事情,那么它是怎么实现的呢?从所周知,匿名类会在编译的时候生成与宿主类带上 $1, $2 的类文件,如写在 TestLamb ...
- web框架--flask
flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...