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. windows下在idea用maven导入spark2.3.1源码并编译并运行示例

    一.前提 1.配置好maven:intellij idea maven配置及maven项目创建 2.下载好spark源码: 二.导入源码: 1.将下载的源码包spark-2.3.1.tgz解压(E:\ ...

  2. 使用json.dumps转换django queryset的datatime报错问题解决

    转换成json时使用的方法如下: json.dumps(list(models.userlist.objects.values("vu"))) 报错信息如下: Traceback ...

  3. docker 部署 zookeeper+kafka 集群

    主机三台172.16.100.61172.16.100.62172.16.100.63Docker 版本 当前最新版 # 部署zk有2种方法 ## 注意 \后不要跟空格 一 . 端口映射 172.16 ...

  4. 小刘的深度学习---CNN

    前言: 前段时间我在树莓派上通过KNN,SVM等机器学习的算法实现了门派识别的项目,所用到的数据集是经典的MNIST.可能是因为手写数字与印刷体存在一些区别,识别率并是很不高.基于这样的情况,我打算在 ...

  5. Vue+webpack项目中,运行报错Cannot find module 'chalk'的处理

    刚开始用vue + webpack新建项目,在github上下载了一个示例,输入npm init >>>npm run dev 后报错 Cannot find module 'cha ...

  6. css 剩余宽度完全填充

    从网上转的. <html> <head> <meta http-equiv="Content-Type" content="text/htm ...

  7. 定时任务crone表达式demo

    1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...

  8. 调试存储过程:ORA-0131 Insufficient privileges

    http://www.cnblogs.com/empty01/p/5568250.html

  9. [东北师大软工]Week2-作业2:个人项目实战 初步测试结果

    作业地址 https://edu.cnblogs.com/campus/nenu/2016SE_NENU/homework/1656 测试须知 测试机为Windows环境,所有提交到Coding.ne ...

  10. GIT理解

    以前从来没听过GIT,根本不知道是什么东西.老师突然让注册一个GIT帐号,不知道怎么注册, 真有点不知所措了,又听说是全英文的,感觉也是醉了!登录进去看了看,看的似懂非懂,自己 也不敢妄下定论于是上网 ...