题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2988

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)的更多相关文章

  1. HDU 3757 Evacuation Plan DP

    跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...

  2. uvalive 6938 区间dp

    看到n范围和给的区间看着就像区间dp 然后怎么cmp感觉都没法进行区间合并 n的300误导了下 没有注意离散化之后对时间可以dp 然而这个dp感觉不太经得起证明的样子... dp[i][j] -> ...

  3. hdu 4412 Sky Soldiers(区间DP)

    Sky Soldiers Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  5. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  6. POJ2175 Evacuation Plan

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4617   Accepted: 1218   ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  9. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. python学习 数据类型之序列

    一.序列(本文使用python3.5)############################################################# 列表.元组 字符窜都是序列#特点:#1 ...

  2. require.js 的使用

    一.为什么要用require.js 在同一个页面要加载多个js文件时,浏览器会停止网页渲染,加载文件越多,网页失去响应的时间就会越长: 其次,由于js文件之间存在依赖关系,因此必须严格保证加载顺序(比 ...

  3. 那些年我们写过的T-SQL(上篇)

    在当今这个多种不同数据库混用,各种不同语言不同框架融合的年代(一切为了降低成本并高效的提供服务),知识点多如牛毛.虽然大部分SQL脚本可以使用标准SQL来写,但在实际中,效率就是一切,因而每种不同厂商 ...

  4. 如果正确读取SQL Server中的扩展事件?

        SQL Server中使用扩展事件捕捉所需的信息后,可以选择存放的位置.比如说内存或文件中,但无论存在哪里,其本质都是一个大XML.因此在SQL Server中读取该XML就是解析扩展事件结果 ...

  5. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

  6. [译]对 AngularJS 模板的A/B测试

    编者按:本文翻译自 Andrei Bondarev 在 Medium 上发布的"A/B Testing your AngularJS Templates ",Andrei 是一名工 ...

  7. 理论到实践,A/B测试不得不直面的4个统计学问题

    有放回?无放回? 从总体中随机抽取一个容量为n的样本,当样本容量 n足够大(通常要求n ≥30)时,无论总体是否符合正态分布,样本均值都会趋于正态分布.期望和总体相同,方差为总体的1/n.这即是中心极 ...

  8. iOS_UIImage_裁切圆形头像

    github地址: https://github.com/mancongiOS/UIImage.git UIImage的Cagetory UIImage+ImageCircle.h - (UIImag ...

  9. $.when(deferreds)

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong ) 1 引子 上一篇博文中介绍的Deferred,它表示一个延迟对象.但是很多时候,我们需要在多个延迟对象(异步代 ...

  10. Dagger2 生成代码学习

    接上一篇文章介绍了Dagger2的初步使用,相信刚接触的人会觉得很奇怪,怎么会有很多自己没有定义的代码出现,为什么Component的创建方式是那样的.为了搞清楚这些东西,我们需要查看一下Dagger ...