【poj3375】 Network Connection
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的更多相关文章
- 【BZOJ】【1834】【ZJOI2010】Network 网络扩容
网络流/费用流 这题……我一开始sb了. 第一问简单的最大流…… 第二问是要建费用流的图的……但是是在第一问的最大流跑完以后的残量网络上建,而不是重建…… 我们令残量网络上原有的弧的费用全部为0(因为 ...
- 【树形贪心】【UVA1267】Network
重要意义:复习好久没写的邻接表了. Network, Seoul 2007, LA3902 Consider a tree network with n nodes where the interna ...
- 【OpenStack】network相关知识学习
network 类型 local:通信不跨主机,必须同一网段,主要做单机测试使用: flat:统计可以跨主机,但是需要在同一网段: 每个 flat network 都会独占一个物理网卡 计算节点上 b ...
- 【转】[Network] 计算机网络基础知识总结
阅读目录 1. 网络层次划分 2. OSI七层网络模型 3. IP地址 4. 子网掩码及网络划分 5. ARP/RARP协议 6. 路由选择协议 7. TCP/IP协议 8. UDP协议 9. DNS ...
- 【原创】Your Connection is not private
用Chrome打开google等https网站时碰到问题: “your connection is not private”. 后来发现是跟GoAgent的安全证书有关系(我用XX.NETFQ) 解决 ...
- 【图论】Network of Schools
[POJ1236]Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18969 Acc ...
- 【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 ...
- 【MongoDB】The connection between two tables
In mongoDB, there are two general way to connect with two tables. Manual Connection and use DBRef 1. ...
- 【leetcode】Network Delay Time
题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...
随机推荐
- HDFS文件系统基础
HDFS架构实现 Hadoop当前稳定版本是Apache Hadoop 2.9.2,最新版本是Apache Hadoop 3.1.1. http://hadoop.apache.org/docs/ H ...
- Final阶段版本控制报告
版本控制代码及文档要求 在coding.net版本控制; 公开项目,教师.专家.其他同学可以不注册源代码.在此公布git地址. 报告beta阶段2周中,项目的版本控制情况,不包括未在coding.ne ...
- Daily Scrumming* 2015.10.24(Day 5)
一.总体情况总结 从今天开始,我们开始正式进入紧锣密鼓的集中开发周啦~~加油Fighting~ 开会讨论了一下各个人的细致分工,前端后端各自想成员分派任务. 继续各自领域的准备工作,同时开始进行开发. ...
- Prim's Algorithm & Kruskal's algorithm
1. Problem These two algorithm are all used to find a minimum spanning tree for a weighted undirecte ...
- Java自学基础用法
在慕课上面简单学习了一下java语言的用法 简单的用法总结记录一下. 代码(学习输入,输出): package hello; import java.util.Scanner; public clas ...
- JSPatch 原理
原理 JSPatch用iOS内置的JavaScriptCore.framework作为JS引擎,但没有用它JSExport的特性进行JS-OC函 数互调,而是通过Objective-C Runtime ...
- fx投影效果分离
虽然忙着花花二期三期bug.bug ing,修改等待中突然看见一张logo.文字下面的阴影图,如下,就满脑子在想阴影到底咋做的.. 七拼八凑的尝试后大体样子是有,终究没有上图那种字体轮廓的阴影... ...
- git常用命令复习及其基本使用示例
年后回来新上到项目,对于git的一些操作命令记得有点混乱了,所以特整理笔记如下: 一.git常用命令复习 查看当前分支:git branch (显示结果中带有*号的是当前分支)查看所有分支: git ...
- Robot Framework 教程 (7) - 使用For循环
在自动化测试过程中,使用For循环来对某个动作进行重复操作是很普遍的行为.在Robot Framework中,各种测试库中均提供了多种方式的For循环结构,在其中覆盖了大部分类型的循环类型.而Robo ...
- testNg-build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <proje ...