Description

There are \(M\) network interfaces in the wall of aisle of library. And \(N\) computers next to the wall need to be connected to the network. A network interface can only connect with one computer at most. To connect an interface with coordinate \(x\) with a computer with coordinate \(y\) needs \(|x - y|\) unit of length of network cable. Your task is to minimize the total length of network cables to be used.

Input

The first line contains two integers \(M (1 ≤ M ≤ 100000), N (1 ≤ N ≤ 2000, N ≤ M)\). The following \(M + N\) lines each contains a integer coordinate. The first \(M\) coordinates are corresponding to the network interfaces, and the next \(N\) ones corresponding to the computers. All coordinates are arranged in \([0, 1000000]\). Distinct interfaces may have the same coordinate, so do the computers.

Output

Print an integer, representing minimum length of network cables to be used.

Sample Input

4 2

1

10

12

20

11

15

Sample Output

4

排序+离散化。

状态\(f[i][j]\)表示考虑前\(i\)台电脑,最后一台插在第\(j\)个接口上的最小代价。转移$$f[i][j] = \min_{i-1 \le k < j} { f[i-1][k]+\mid A_i-B_j \mid }$$可用前缀和优化,复杂度\(O(NM)\)。

减少无用状态:每台电脑的接口一定不会离他很远。假设离\(i\)最近的接口为\(j\),只需要枚举\(j-N \sim j+N\)这些接口即可,这里面一定有些是空的。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
const int maxn = 2010,maxm = 100010;
const ll inf = 1LL<<60;
ll f[2][maxm],ans = inf; int N,M,A[maxm],B[maxn]; inline int gi()
{
char ch; int ret = 0,f = 1;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
} int main()
{
freopen("3375.in","r",stdin);
freopen("3375.out","w",stdout);
M = gi(); N = gi();
for (int i = 1;i <= M;++i) A[i] = gi();
for (int i = 1;i <= N;++i) B[i] = gi();
sort(A+1,A+M+1); sort(B+1,B+N+1);
for (int i = 0;i <= M;++i) f[1][i] = inf;
for (int i = 1,now = 1,last = 0,Lastl = 0,Lastr = M;i <= N;++i,swap(now,last))
{
int pos = lower_bound(A+1,A+M+1,B[i])-A,l = max(1,pos-N-1),r = min(M,pos+N+1);
for (int j = l;j <= r;++j) f[now][j] = min(f[now][j-1],f[last][min(j-1,Lastr)]+(ll)abs(A[j]-B[i]));
for (int j = Lastl;j <= Lastr;++j) f[last][j] = inf;
Lastl = l; Lastr = r;
}
for (int i = 1,now = N&1;i <= M;++i) ans = min(ans,f[now][i]);
cout << ans << endl;
fclose(stdin); fclose(stdout);
return 0;
}

poj3375 Network Connection的更多相关文章

  1. RobotFramework自动化测试框架-移动手机自动化测试Get Network Connection Status和Set Network Connection Status关键字的使用

    Get Network Connection Status关键字用来获取手机的网络连接状态.在获取到连接状态后,会返回不同的数字. Set Network Connection Status关键字用来 ...

  2. This network connection does not exist

    This network connection does not exist 在windows server 2008上面map了一个磁盘,共享的folder被我停止共享后,点击该磁盘的disconn ...

  3. 初次使用git就遭遇不测,提示没有这个服务连接和需要配置git的一个http参数 NO network connection,SSl host could not be verified ...

    第一次使用git 拉取服务上的项目到本地,结果,在拿到访问的url地址后,输入用户名密码,失败了. --eclispe  4.5.3 继承了git客户端插件的版本 ------下一步后,报错 NO n ...

  4. Learning Experience of Big Data:The First Day-Try to set up a network connection on my virtural machine

    After we install our virtual machine,the first thing we should do is to set up a network connection ...

  5. Network Connection Lost When Windows 8 Goes To Sleep

    http://www.kapilarya.com/fix-network-connection-lost-when-windows-8-goes-to-sleep http://superuser.c ...

  6. The network connection was lost 文件下载错误提示

    假设出现这种错误,可能是模拟器断网,重新启动下模拟器就能够:The network connection was lost

  7. Adjusting Network Connection

    Adjusting Network Connection The Selenium Mobile JSON Wire Protocol Specification supports an API fo ...

  8. 使用putty连接虚拟机上的centos提示Network:connection refused

    转自:https://yeyuan.iteye.com/blog/1266484 今天早上开机之后,像往常一样使用putty连接linux的时候,突然提示Network:connection refu ...

  9. MATLAB实例:构造网络连接图(Network Connection)及计算图的代数连通度(Algebraic Connectivity)

    MATLAB实例:构造网络连接图(Network Connection)及计算图的代数连通度(Algebraic Connectivity) 作者:凯鲁嘎吉 - 博客园 http://www.cnbl ...

随机推荐

  1. YII2 不通过composer安装Ueditor编辑器

    今天用composer安装Ueditor,一直下载失败,不知道为什么,所以就手动安装了一下.记录一下安装步骤 GitHub地址 https://github.com/BigKuCha/yii2-ued ...

  2. fabric Report API

    1.Token生成 接口 : post https://fabric.io/oauth/token 请求头:Headers Content-Type : application/json 正文: bo ...

  3. 爬虫之requests模块基础

    一.request模块介绍 1. 什么是request模块 - python中原生的基于网络请求的模块,模拟浏览器发起请求. 2. 为什么使用request模块 - urllib需要手动处理url编码 ...

  4. 什么是高防服务器?如何搭建DDOS流量攻击防护系统

    关于高防服务器的使用以及需求,从以往的联众棋牌到目前发展迅猛的手机APP棋牌,越来越多的游戏行业都在使用高防服务器系统,从2018年1月到11月,国内棋牌运营公司发展到了几百家. 棋牌的玩法模式从之前 ...

  5. Linux C语言结构体-学习笔记

    Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...

  6. xampps 不能配置非安装目录虚拟主机解决方案

    今天将前几天安装好的xampps配置下,准备开始php开发之旅,在我信心满满的将工作目录定在非安装目录上(安装目录在:D:\Program Files\xampps\apache\htdocs  我将 ...

  7. Fiddler 发送post 请求失败

    今天服务端同事,让我发一个post 请求.然后呢,一直有问题.告诉我签名失败. 后来换了其他的在线模拟post,都是可以的. 后来找到原因了, post 请求,必须要有Content-Type 和 C ...

  8. ip4addr_ntoa和不可重入函数

    在网络中,有一个转换IP地址到ASIIC字符串的函数,该函数的返回值所指向的ASIIC字符串驻留在静态内存中,所以该函数不可重入. 通俗的讲,在多任务系统中,一个任务执行在调用运行这个函数的时候,其他 ...

  9. Can’t delete list item in Sharepoint2013

         Today,I have meet a very strange error.When I attempt to delete a item from a list,I recieve an ...

  10. leetcode 笔记5 single number

    question: Given an array of integers, every element appears twice except for one. Find that single o ...