【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 ...
随机推荐
- Playfair加密
前面讲的不管是单码加密还是多码加密都属于单图加密,什么是单图加密和多图加密呢,简单来说单图加密就是一个字母加密一个字母,而多图加密就是一个字符组加密一个字符组.比如双图加密就是两个字母加密两个字母,这 ...
- spring-boot+swagger实现WebApi文档
1.引用依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
- 完整Highchart JS示例
线性: $.ajax({ type:'post', url:appPages.urlListTjrll, cache:false, data:{year:year,month:month},// // ...
- LeetCode 404. Sum of Left Leaves (C++)
题目: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are t ...
- 必应词典手机版(IOS版)与有道词典(IOS版)之软件分析【功能篇】【用户体验篇】
1.序言: 随着手机功能的不断更新和推广,手机应用市场的竞争变得愈发激烈.这次我们选择必应词典和有道词典的苹果客户端作对比,进一步分析这两款词典的客户端在功能和用户体验方面的利弊.这次测评的主要评测人 ...
- BugPhobia回顾篇章:团队Alpha阶段工作分析
0x00:序言 1 universe, 9 planets, 204 countries,809 islands, 7 seas, and i had the privilege to meet yo ...
- 求int型数组和最大子数组 续
之前的博文里已经实现过该程序的构思.编译.运行,本次就不再重复与之相雷同的内容. 题目:与别人借组,借助求int型数组最大和子数组的问题,考虑大数溢出和int取值范围的问题 要求: 调试程序 当子数 ...
- 【搜索】POJ-2718 全排列+暴力
一.题目 Description Given a number of distinct decimal digits, you can form one integer by choosing a n ...
- 福大软工 · 第七次作业 - 需求分析报告(404 Note Found队)
目录 组队后的团队项目的整体计划安排 项目logo及思维导图 项目logo 思维导图 产品思维导图 产品思维导图-引导 产品思维导图-后端数据处理.存储 产品思维导图-短信识别 产品思维导图-智能分析 ...
- 深入理解JAVA集合系列二:ConcurrentHashMap源码解读
HashMap和Hashtable的区别 在正式开始这篇文章的主题之前,我们先来比较下HashMap和Hashtable之间的差异点: 1.Hashtable是线程安全的,它对外提供的所有方法都是都使 ...