POJ1160 Post Office[序列DP]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 18680 | Accepted: 10075 |
Description
Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.
You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.
Input
Output
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int V=,P=,INF=1e9;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int v,p,a[V],l[V][V],f[V][P];
void dp(){
for(int i=;i<=v;i++)
for(int j=i+;j<=v;j++)
l[i][j]=l[i][j-]+a[j]-a[(i+j)/];
for(int i=;i<=v;i++) for(int j=;j<=v;j++) f[i][j]=INF,f[i][]=l[][i];
for(int i=;i<=v;i++)
for(int j=;j<=p;j++){
for(int k=;k<i;k++)
f[i][j]=min(f[i][j],f[k][j-]+l[k+][i]);
}
}
int main(int argc, const char * argv[]) {
v=read();p=read();
for(int i=;i<=v;i++)a[i]=read();
dp();
printf("%d",f[v][p]);
return ;
}
POJ1160 Post Office[序列DP]的更多相关文章
- POJ-1160 Post Office (DP+四边形不等式优化)
题目大意:有v个村庄成直线排列,要建设p个邮局,为了使每一个村庄到离它最近的邮局的距离之和最小,应该怎样分配邮局的建设,输出最小距离和. 题目分析:定义状态dp(i,j)表示建设 i 个邮局最远覆盖到 ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [OpenJudge90][序列DP+乱搞]滑雪
滑雪 总时间限制: 1000ms 内存限制: 65536kB [描述] Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次 ...
- 序列DP(输出有要求)
DP Time Limit:10000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status De ...
- hdoj5909 Tree Cutting(点分治+树上dp转序列dp)
题目链接:https://vjudge.net/problem/HDU-5909 题意:给一颗树,结点带权值v[i]<m.求异或和为k的子树个数(0<=k<m). 思路: 首先点分治 ...
- 一类巧妙利用利用失配树的序列DP
I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...
- POJ1160 Post Office (四边形不等式优化DP)
There is a straight highway with villages alongside the highway. The highway is represented as an in ...
- [POJ1160] Post Office [四边形不等式dp]
题面: 传送门 思路: dp方程实际上很好想 设$dp\left[i\right]\left[j\right]$表示前$j$个镇子设立$i$个邮局的最小花费 然后状态转移: $dp\left[i\ri ...
- 洛谷P1415 拆分数列[序列DP 状态 打印]
题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...
随机推荐
- 性能更好的js动画实现方式——requestAnimationFrame
用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是 ...
- (转)轻松学习JavaScript三:JavaScript与HTML的结合
摘自:http://blog.csdn.net/erlian1992 HTML中的JavaScript脚本必须位于<script>与</script>标签之间,JavaScri ...
- [deviceone开发]-一个很炫的手势动画示例
一.简介 这是iOS下的效果,android下完全一致.通过do_GestureView组件和do_Animation组件,deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过手 ...
- [deviceone开发]-echart的简单报表示例
一.简介 echart是一个常用的基于h5的报表库.这个例子简单展示了实现折线图,柱状图,圆环图和圆饼图的使用. 并实现和do的非html部分的数据交互. 二.效果图 三.相关下载 https://g ...
- asp.net面试题汇总
1.静态成员和非静态成员的区别? 答: 静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对 ...
- 设置text-overflow文本溢出隐藏时的对齐问题
设置text-overflow: ellipsis后引起的文本对齐问题 最近在做网页的时候用到了文本溢出隐藏的功能,但是出现了一些小问题,下面先放上示例代码吧. <p> <span ...
- 记录一下dotnetcore.1.0.0-VS2015Tools.preview2安装不上的问题
错误提示:未指定错误 解决方案: 从这里下载:https://visualstudiogallery.msdn.microsoft.com/32f1fa1b-cdd5-4bd3-8f51-cd8f09 ...
- 创建SAP GUI快捷方式保存密码
1.在注册表中创建GUI 快捷方式的子键 a.首先运行 微软标识键+R b.窗口中输入sapshcut,如果有窗口跳出点击“确定” 2.维护子键下的键值 a.再次运行 微软标识键+R ...
- Python统计学技术环境
http://www.lfd.uci.edu/~gohlke/pythonlibs/#sympy 1.1. Python 1.1.1. NumPy pip install numpy-1.11.0+m ...
- Autodesk的照片建模云服务—Autodesk ReCap 360 photo 的测试数据
之前已经给大家介绍过了Autodesk的照片建模云服务—Autodesk ReCap 360 photo, 你也可以自己登录到http://recap360.autodesk.com/ 自己试一试. ...