poj 1160 Post Office

POJ 1160 Post Office(经典DP) 题目链接:点击打开链接 题意:一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最后打印这个最小值。 思路:典型的DP问题。 当我们在v个村庄中只建一个邮局,可以推导出,只有邮局位于中间位置,距离和才最小;有一个特殊情况是,当村庄数为偶数,中间位置有两个村庄,经过计算,两个村庄的距离和相等,所以俩位置均可。 可以联 阅读详情

//POJ 1160 解题报告:(引用以下两位大牛,写得不错)
//http://www.608088.com/show-51-1.html
//http://hi.baidu.com/rain_bow_joy/blog/item/ba85e922219b455b9822edfd.html
#include <cstdio>
#include <cmath>

int dist[310][310];
int v[310];
int dp[310][310];

int main()
{
 int n,m;
 scanf("%d%d",&n,&m);
 for (int i=1;i<=n;i++)
 {
  scanf("%d",&v[i]);
 }

 for (int i=1;i<=n;i++)
  for (int j=i+1;j<=n;j++)
  {
   int mid=(i+j)>>1;
   for (int k=i;k<=j;k++)
    dist[i][j]+=abs(v[k]-v[mid]);
  }


 for (int i=1;i<=m;i++)
  for (int j=1;j<=n;j++)
   dp[i][j]=9999999;

 for (int i=1;i<=n;i++)
  dp[1][i]=dist[1][i];

 for (int i=2;i<=m;i++)
  for (int j=i;j<=n;j++)
  {
   for (int k=i;k<=j;k++)
    if (dp[i][j]>dp[i-1][k-1]+dist[k][j])
     dp[i][j]=dp[i-1][k-1]+dist[k][j];
  }


 printf("%d/n",dp[m][n]);
 return 0;
}
   

 

 

poj——1160——Post Office 题目:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择P个村庄在其中建立邮局,要求每个村庄到最近邮局的距离和最小。 分析:经典DP 1、考虑在V个村庄中只建立一个邮局的情况,显然可以知道,将邮局建立在中间的那个村庄即可。也就是在a到b间建立一个邮局,若使消耗最小,则应该将邮局建立在(a+b)/2这个村庄上(可以通过画图知道)。 2、下面考虑建立多个邮局的问题,可以 阅读详情

相关推荐

dp四边形优化 poj 1160 Post Office题解

累加器传送门:http://blog.csdn.net/NOIAu/article/details/71775000题目传送门:https://cn.vjudge.net/problem/POJ-1160四边形优化算法讲解:http://blog.csdn.net/NOIAu/article/details/72514812题目:There is a straight highway with vi

Monica 1388

POJ 1160 Post Office(区间DP)

poj 1160 Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21315   Accepted: 11522 Description There is a straight highway with villages alongside the highway...

Kruskal的博客 650

POJ 1160:Post Office 邮局经典DP

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17168   Accepted: 9270 Description There is a straight highway with villages alongside the high

2553

POJ1160 Post Office题解

POJ1160 Post Office 传送门 题目描述 在笔直的大道上建有nnn个大别墅,两个大别墅之间的距离是他们的横坐标之差的绝对值,保证大别墅之间的距离只能是整数,且没有别墅的位置是相同的。 现在需要建立mmm座大超市(m≤n)(m \leq n)(m≤n),大超市只能建立在别墅所在的位置,现在需要你写个程序,给定了所有别墅的位置和大超市的数目,计算出每个别墅离最近的大超市的距离总和的最小值。 输入格式 第一行包括两个整数nnn和mmm 第二行包括n个整数,代表别墅的位置,以升序的形式列出。

CoderZeng的博客 428

poj1160DP(POST OFFICE 邮局问题)

题目链接: /*poj1160 题意:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上, 需要选择P个村庄在其中建立邮局,要求每个村庄到最近邮局的距离和最小。 状态:d[i][j]:前i个村庄建j个邮局的最小花费 转移方程:d[i][j] = min(d[i][j], dp[k][j-1]+ dis[k+1][i]); dis[i][j]表示村庄i到村庄j之间建一个邮局的花费

no pains,no gains 1422

POJ--1160--Post Office

这是一道经典的DP题,数组开小了,POJ报WA,调试方向错了,调10年都是错的,要是在比赛的时候紧张的环境就更不容易发现错误了,就觉得在任何情况下保持冷静的头脑是一件很难得的事。 题目大意:给定在x轴上的N个村庄(就是所有村庄在一条直线上面),在这N 个村庄中选出M 个点作为邮局,使得每个村庄到最近的邮局的距离和最小,求出这个最小的距离和。 解题报告:假设在N 个村庄中选择一个村庄作为邮局使得

WoniuGe 614

POJ 1160 Post Office

POJ 1160 Post Office 经典的四边形不等式优化,可以达到O(VP)。不过网上很多人的程序对四边形不等式应用的不好,他们那么写是达不到O(VP)的。 //By YY_More #include<cstdio> int x[301...

aimifeng9177的博客 187

商店销售预测(回归&随机森林)

通过准备线性回归的数据,我们发现了一些有趣的见解,包括周末的销售增长,公共部门支付工资时的销售增长,以及11月和12月的销售增长。如果我们选择包括这些,我们可能会得到更好的结果,但在这个时候,我们不会用它来做任何预测,而是将转向另一种可能给我们更好的结果的技术。在Kaggle竞赛中,要求我们应用时间序列预测,根据厄瓜多尔大型杂货零售商Corporación Favorita的数据预测商店销售情况,建立一个模型,准确地预测在不同商店销售的商品的单位销量。在快速查看我们的训练数据集之后,我们可以看到有很多零。

qq_45791012的博客 4760

【POJ 1160】Post Office

四边形优化dp转移经典题目~

Regina8023 1895

Linux系统centos7安装网卡驱动

有时候我们装完Centos或Ubuntu系统会发现没有网卡信息,说明网卡驱动不配置,所以我们要重新安装。 首先去服务器主板官网下载网卡驱动,注意32位和64位。 先查看网卡型号 lspci | grep -i ethernet 我的网卡型号是 00:1f.6 Ethernet controller: Intel Corporation Device 0d55。 网上一找,压根没找到这型号的网卡,别说驱动了。怀着试一试的心态,就安装i219-v系的网卡驱动,还别说,成功了。 针对i21...

wshc18的博客 2万+

mysql数据库中使用存储过程带来的好处和示例(存储过程的概念、定义、作用等详解)

MySQL的存储过程(Stored Procedure)是一组SQL语句集合。用户通过指定的存储过程名和参数来调用并执行它。存储过程可以在数据库中预编译并存储,这样它们可以被多次调用而无需重新编写或解析SQL语句。 存储过程将复杂的SQL逻辑封装在一个可重用的单元中。多次调用存储过程,而不需要重复编写相同的SQL代码。- 通过封装,可以隐藏复杂的SQL逻辑,只向用户或应用程序提供简单的接口。MySQL的存储过程和编程语言中的函数的原理和目标是一致的:封装可重用的代码逻辑并提高代码的可维护性和可重用性。

weixin_70208651的博客 1723

POJ - 1160 Post Office

POJ - 1160 思路: 平行四边形不等式优化dp dp[i][j]:前j个选i个作为邮局的最小答案 w[i][j]:i到j之间选一个作为邮局的最小距离和,肯定是选中间的 dp[i][j] = min{dp[i-1][k] + w[k+1][j]} 这个方程和石子归并类似,满足四边形不等式(一般打表找规律,不推不等式),所以决策变量满足决策单调性 假设s[i][j]为dp[i]...

weixin_34262482的博客 159

visionpro官方示例分析(一) 模板匹配工具 缺陷检测工具

使用CogPMAlignTool工具,该工具是模板匹配工具,见名知意,所谓模板匹配工具就是说先使用该工具对一张图像建立模板,然后用这个模板在其他图像上进行匹配,匹配上了就说明找到了,匹配不上就说明没有找到。

sc_1313的博客 1931

POJ1160, post office

POJ1160, post office。动态规划的经典题目。呃,又是经典题目,DP部分的经典题目怎就这么多。木有办法,事实就这样。 求:在村庄内建邮局,要使村庄到邮局的距离和最小。 设有m个村庄,分别为 V1 V2 V3 … Vm, 要建n个邮局,分别为P1 P2 P3 … Pn。 在DP的问题中,经常有从m个物体中选n个物体的情况,本题显然也属于这种情况。一般可以这样考虑:假

sjyttkl的专栏 662

AArch64 ARM64 寄存器介绍

除了上述通用寄存器,还有FPSR(浮点状态寄存器)和 FPCR(浮点控制寄存器)等。X29寄存器和X30寄存器在函数调用和堆栈帧的管理中扮演着关键角色。AArch64架构提供了31个通用寄存器,每个寄存器都可以用作64位X寄存器(X0~X30)或32位W寄存器(W0~W30)。使用X寄存器将导致64位的计算,使用W寄存器将导致32位的计算。EL0、EL1、EL2和EL3是AArch64架构中定义的四个特权级别(Exception Levels),用于支持不同的执行环境和特权级别。

lml435035844的博客 1万+

R语言基于机器学习三分类结果、计算三分类混淆矩阵、计算每一个类别对应的特异度(specificity)、灵敏度(sensitivity)、PPV、NPV

R语言基于机器学习三分类结果、计算三分类混淆矩阵、计算每一个类别对应的特异度(specificity)、灵敏度(sensitivity)、PPV、NPV

data+scenario+science+insight 667

Post Office POJ - 1160

Post Office  POJ - 1160  There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a

九大于七的博客 333

ROS 之 使用yocs_velocity_smoother对机器人速度进行限制

yocs_velocity_smoother是一个速度、加速度限制器,用来防止robot navigation的速度/转速过快,加速度/快减速过大。Bound incoming velocity messages according to robot velocity and acceleration limits. The velocity smoother nodelet runs together with the kobuki_node to apply robot’s velocity and .

https://shixuebin.gitee.io/hexo-blog/ 2630

poj1160Post Office

链接:http://poj.org/problem?id=1160 题意:有n个村庄在一条直线上,能建k个邮局,求所有村庄到离它最近的邮局的总和最小。 分析:四边形不等式优化dp的第一题。赵爽的动态规划加速原理之四边形不等式不错。对于这题我们设dp[i][j]表示前i个村庄建j个邮局的总和最小为多少,w[i][j]表示在第i个村庄到第j个村庄中建一个邮局并且i~j都去这个邮局的最小距离,那么w

Fsss 696

【大模型】AI 辅助编程操作实战使用详解

AI 编程实战使用详解

congge 8472

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

POJ1160 【Post Office】

POJ1160 【Post Office】DescriptionThere is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a sin

This is Mendez. 1413

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

poj 1160 Post Office

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13983   Accepted: 7540 Description There is a straight highway with villages alongside the highwa

记录成长的点点滴滴. . . . . 1426

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj1160 Post Office

http://www.elijahqi.win/2018/01/12/poj1160-post-office/ Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position o

Elijahqi 348
上一篇: poj 1276 Cash Machine
下一篇: poj 1088 滑雪
birdforever
博客等级 码龄16年 4粉丝 83原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值