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 状态 打印]
题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...
随机推荐
- 定时器相关 setTimeout setInterval 函数节流
这个问题也是在参加百度的前端技术学院中遇到的 任务中需要用js实现动画 导师给的评价中setInterval会导致bug 当时不理解 下面把自己学习的过程分享出来 再次理解单线程 老是说js ...
- Eclipse Plug-in Hello world
这一篇就简单说下一个hello world插件工程创建过程. 1.创建一个Plug-in Project 2.填写project name 3.第二个确认框勾上,然后直接下一步 ...
- vue单页面程序
gitHub地址:https://github.com/lily1010/vue_singlePage 举个栗子: <!DOCTYPE html> <html> <hea ...
- kali 忘记登录密码后重置的方法
首先启动你的卡里系统,等出现引导界面时选择恢复模式.如下图: 再出来一个界面时,选择第二个并按E键进入编辑模式.如下图: 进入编辑模式后找到如下图的代码: 把ro改为rw,并且在.gz 后面写上ini ...
- SharePoint2013 此产品的试用期已结束
今天使用SharePoint 2013创建页面的时候,突然提示“此产品的试用期已结束 ”. 网上解决办法: “将IIS的‘应用程序池’下网站集对应的‘宿主应用程序’的‘应用程序池标识’改为‘域管理员或 ...
- iOS中的UI
• 不管你是学习android开发还是iOS开发• 都建议先学习UI,原因如下:UI是app的根基:⼀一个app应该是先有UI界⾯面,然后在UI的基础上增加实⽤用功能 UI相对简单易学:UI普遍是学 ...
- Selenium Test 自动化测试 入门级学习笔记
1.下载安装Firefox-selenium插件 需要下载插件可以联系,这里暂不提供下载地址. 2.集成Eclipse 需要下载jar包可以联系,这里暂不提供下载地址. 集成Eclipse非常简单,加 ...
- HttpServletResponse对象 学习
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- git merge git pull时候遇到冲突解决办法git stash
在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...
- 正确的选择log级别
开发一个应用,日志的重要性不言而喻.然而有时会发现日志中会出现大量的垃圾日志.所谓垃圾日志,就是不需要知道的日志,或者这些日志对于应用查看.跟踪没有什么作用.也正是(但不仅仅是)出于这些问题的考量,常 ...