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 ...
随机推荐
- piap.windows io 监测attilax总结
piap.windows io 监测attilax总结 当硬盘光狂闪的时候. 主要目标:找出哪个进程占用io最多, 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来 ...
- fir.im weekly - 「 持续集成 」实践教程合集
我们常看到许多团队和开发者分享他们的持续集成实践经验,本期 fir.im Weekly 收集了 iOS,Android,PHP ,NodeJS 等项目搭建持续集成的实践,以及一些国内外公司的内部持续集 ...
- Asp.net WebApi 项目示例(增删改查)
1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...
- IIS集成模式下,URL重写后获取不到Session值
近期给公司网站添加了伪静态功能,但是今天发现了在伪静态的页面中,Session值是获取不到的. 原因是在伪静态请求的时候,Session请求被“过滤”掉了. 开始是把web.config文件中的mod ...
- Python中的魔法方法
1.什么是魔法方法? 魔法方法就是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一 ...
- 快速入门系列--CLR--01基本概念
在.NET平台用C#这么久,自然会发现其版本很多,相应的概念也会很多,常常都是萌萌哒.而在实际工作中经常会遇到需要配置dll版本号,公钥token等场景,因而对C#.NET.CLR.框架类型等基础概念 ...
- 快速入门系列--MySQL
一直说要好好复习一下Mysql都木有时间,终于赶上最近新购买了阿里云,决定使用CentOS去试试.NET Core等相关的开发,于是决定好好的回顾下这部分知识,由于Mysql的数据库引擎是插件式的,对 ...
- ValidationSummary控件不弹出错误提示框
采用VS2013 编写的前台,运用ValidationSummary控件时,不出现错误弹窗,网上找到了解决方法 发现是ASP.NET 4.5对验证控件的影响(兼容性),使用ASP.NET 4.5的解决 ...
- Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet
概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...
- Yii的学习(4)--Active Record
摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 ...