http://poj.org/problem?id=3375 (题目链接)

题意

  有$M$个网络接口和$N$台计算机,给出它们的坐标(在同一直线上),一个接口只能接一台计算机,费用为两坐标之差的绝对值,问最小费用为多少。

Solution

  $f[i][j]$表示前$i$台计算机连在前$j$个网络接口上的最小费用。转移:$$f[i][j]=min(f[i][j-1],f[i-1][j-1]+|X[i]-X[j]|)$$

  考虑$m$的范围很大,肯定有很多是无用的,我们找到距离$i$最近的接口$pos$,从$N-pos$ for到$N+pos$即可。

细节

  LL

代码

// poj3375
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf (1ll<<60)
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=200010;
int t1[maxn],t2[maxn],pos[maxn],n,m;
LL f[2][maxn]; int main() {
scanf("%d%d",&m,&n);
for (int i=1;i<=m;i++) scanf("%d",&t2[i]);
for (int i=1;i<=n;i++) scanf("%d",&t1[i]);
sort(t1+1,t1+n+1);
sort(t2+1,t2+m+1);
for (int i=1;i<=n;i++) pos[i]=lower_bound(t2+1,t2+1+m,t1[i])-t2;
memset(f,0x7f,sizeof(f));
for (int i=0;i<=m;i++) f[0][i]=0;
int p=0,q=1,l,r,u=0,v=m;
for (int i=1;i<=n;i++) {
l=max(1,pos[i]-n),r=min(m,pos[i]+n);
p^=1,q^=1;
for (int j=l;j<=r;j++) f[p][j]=min(f[p][j-1],f[q][min(j-1,v)]+abs(t1[i]-t2[j]));
for (int j=u;j<=v;j++) f[q][j]=inf;
u=l,v=r;
}
printf("%lld",f[p][r]);
return 0;
}

【poj3375】 Network Connection的更多相关文章

  1. 【BZOJ】【1834】【ZJOI2010】Network 网络扩容

    网络流/费用流 这题……我一开始sb了. 第一问简单的最大流…… 第二问是要建费用流的图的……但是是在第一问的最大流跑完以后的残量网络上建,而不是重建…… 我们令残量网络上原有的弧的费用全部为0(因为 ...

  2. 【树形贪心】【UVA1267】Network

    重要意义:复习好久没写的邻接表了. Network, Seoul 2007, LA3902 Consider a tree network with n nodes where the interna ...

  3. 【OpenStack】network相关知识学习

    network 类型 local:通信不跨主机,必须同一网段,主要做单机测试使用: flat:统计可以跨主机,但是需要在同一网段: 每个 flat network 都会独占一个物理网卡 计算节点上 b ...

  4. 【转】[Network] 计算机网络基础知识总结

    阅读目录 1. 网络层次划分 2. OSI七层网络模型 3. IP地址 4. 子网掩码及网络划分 5. ARP/RARP协议 6. 路由选择协议 7. TCP/IP协议 8. UDP协议 9. DNS ...

  5. 【原创】Your Connection is not private

    用Chrome打开google等https网站时碰到问题: “your connection is not private”. 后来发现是跟GoAgent的安全证书有关系(我用XX.NETFQ) 解决 ...

  6. 【图论】Network of Schools

    [POJ1236]Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18969   Acc ...

  7. 【Kubernetes】The connection to the server <master>:6443 was refused - did you specify the right host or port?

    不知道怎么用着用着,使用kubectl 时报错,错误如下: root@R740--:~# kubectl get pod The connection to the server 107.105.13 ...

  8. 【MongoDB】The connection between two tables

    In mongoDB, there are two general way to connect with two tables. Manual Connection and use DBRef 1. ...

  9. 【leetcode】Network Delay Time

    题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...

随机推荐

  1. ClassLoader.loadClass()与Class.forName()的区别

    ClassLoader.loadClass()与Class.forName()都是反射用来构造类的方法,但是他们的用法还是有一定区别的. 在讲区别之前,我觉得很有不要把类的加载过程在此整理一下. 在J ...

  2. Ubuntu系统无法识别Logitech M590蓝牙鼠标的问题

    参见 - https://blog.csdn.net/yh2869/article/details/73119018 亲测可用. 系统:ubuntu 16.04 64bit 现象:鼠标配对可以成功,但 ...

  3. Mysql数据库的隔离级别

    Mysql数据库的隔离级别有四种 1.read umcommitted   读未提交(当前事务可以读取其他事务没提交的数据,会读取到脏数据) 2.read committed 读已提交(当前事务不能读 ...

  4. 基于NABCD评论作业-王者荣耀交流协会PSP DAILY

    一.根据(不限于)NABCD评论作品的选题   N(Need,需求):在我知道PSP DAILY这款软件的时候,就认为这款软件对于学习软件工程课的学生来说有很大的需要.对于需求来说,软件工程课程中的学 ...

  5. Notes of Daily Scrum Meeting(11.10)

    Notes of Daily Scrum Meeting(11.10) 今天是周一,虽然仍然在假期里,但是我们仍然要继续我们团队的开发工作了,分工大家已然都很明确,所以接下来 就是认真投入,把自己负责 ...

  6. Daily Scrum 11.18

    今日完成任务: 1.在提问问题的时候为问题创建索引 2.解决了修改个人资料后刷新没有更新的问题 3.初步加入了采纳功能(没完善UI设计) 遇到困难:创建索引之后,跳转到主页,需要重新登录,找了半天不知 ...

  7. iOS开发学习-cocoapods的配置安装

    安装coacoapods步骤: 在终端输入如下命令,升级ruby版本: sudo gem update —system 出现这个,标志着安装成功. 完成之后,再输入如下命令: gem sources ...

  8. object-oriented second work

    work request github enter 这次作业做过,不过以前是用数组写的,当我用双向链表写这题时,刚交上去一直出错,后面我又改了改,最后一点一致凑,后面有同学告诉我在构建链表后要判断链表 ...

  9. Oracle 11g R2 for Win7旗舰版(64位)- 安装

    1.下载Oracle 11g R2 for Windows的版本                                   下载地址:http://www.oracle.com/techne ...

  10. asp.net 错误24

    错误 24 “xxx.Web.xxx.xxx”不包含“xxName”的定义,并且找不到可接受类型为“xxx.Web.xxxr.xxx”的第一个参数的扩展方法“xxxName”(是否缺少 using 指 ...