POJ1160 【Post Office】

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 of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

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

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

Output

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

Sample Input

10 5

1 2 3 6 7 9 11 22 44 50

Sample Output

9

Source

IOI 2000


Solution:

DP

状态:f[i][j]:前i个村庄,放j个邮局的最小sum值

输出:f[N][M]

转移方程:f[i][j]=min{f[k][j-1]+sum[k+1][i]}

解释一下:很显然前i个村庄的状态是由之前的状态转移而来的,但我们又不确定邮局的位置以及有无,就需要枚举前k个村庄的状态(其中1<=k<=i),并且前k个村庄一定有j-1个邮局,再加上从k+1~i中建一个邮局的最小sum值

sum[i][j]:表示从i->j中建一个邮局的最小sum值。

如果i->j中只有一个邮局而无别的村庄很显然最小值为pos[j]-pos[i];类比而来如果中间存在两个村庄,我们就要在这两个村庄中建造,这样无限的推理下去,最后会到一个标记中间值建造邮局。

sum[i][j]=sum[i][j-1]+pos[j]-pos[(i+j)/2];

注意

  1. f[i][1]=sum[1][i];
  2. 每次f[i]][j]的初值要附成INF
  3. j在i前枚举,且j>=2

Code:

#include <stdio.h>
#include <string.h>
#define MAXN 500
#define INF 999999
int n,m;
int a[MAXN],sum[MAXN][MAXN],f[MAXN][MAXN];
int min(int a,int b){return a<b?a:b;}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            sum[i][j]=sum[i][j-1]+a[j]-a[(i+j)/2];
        }
    }
    for(int i=1;i<=n;i++)
    {
        f[i][1]=sum[1][i];
    }
    for(int j=2;j<=m;j++)
    {
        for(int i=j+1;i<=n;i++)
        {
            f[i][j]=INF;
            for(int k=1;k<i;k++)
            {
                f[i][j]=min(f[i][j],f[k][j-1]+sum[k+1][i]);
            }
        }
    }
    printf("%d\n",f[n][m]);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 10:53:46

POJ1160 【Post Office】的相关文章

【四边形不等式】POJ1160[IOI2000]-Post Office

[题目大意] v个村庄p个邮局,邮局在村庄里,给出村庄的位置,求每个村庄到最近邮局距离之和的最小值. [思路] 四边形不等式,虽然我并不会证明:( dp[i][j]表示前i个村庄建j个邮局的最小值,w[i][j]表示在i到j之间建立一个邮局的最小值.w[i][j]显然取i~j的中位数,可以在O(1)时间内求出. 显然dp[i][j]=min{dp[k][j-1]+w[k+1][i]}. 傻傻写错i和j…… 1 #include<iostream> 2 #include<cstdio>

【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable

OleDbDataAdapter方式: /// <summary> /// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </summary> /// <param name="strSql"></param>        /// <param name="excelpath">excel路径</param> /// <returns>

poj1161Post Office【经典dp】

题目:poj1161Post Office点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面建p个邮局,村民优先选择去近的邮局,问所有村庄去邮局的最小距离和是多少? 分类:区间dp 分析:对于任意一个村庄,只有两种选择,要么在这儿建邮局,要么不建,我们可以预处理出来任意两件建立一个邮局的的最小距离w[i][j],而对于任意两点,建立一个邮局的最优方案是建立在两点的中位数上,即(i+j)/2,位置. 对于任意两点 i---j ,建立两个邮局的最优结果我们可以由建立一个的得到,

【真正福利】成为专业程序员路上用到的各种优秀资料、神器及框架

转载,原地址:http://www.cnblogs.com/jasondan/p/6380597.html 据说看到好文章不推荐的人,服务器容易宕机!本文版权归翟士丹(Stan Zhai)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利. 好东西不是随便收集下,发篇博文,骗些点赞的!积累了5年多的东西,是时候放出来跟大家见见面了. 或许有的园友在14年的时候收藏过我的一篇"工欲善其事.必先利其器"的博文,时隔3年,已经

系统也就那点事【基础篇】

不知道为什么总是有人不会装系统,我就稍微整理一下:(实战性很强)[本次更新了工具]我敢保证:只要你认真看了你就会了! 网上很多人也在教别人重装系统,可是总是点到即止,还有就是以一总最理想的方式来讲述...试问,有几个人总是这么理想的完成整个过程呢?在装的过程中遇到问题又该咋办呢?————学习一下吧,这个是我当初大一时候录的,当时知识可能有限,如有什么不足的还望海涵... 先简单了解一下:(不要求懂,只要大概了解一下[当时自我研究自我总结的,如果有其他方法可以说的]) 重装系统的几种方法: 标清观

POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12296   Accepted: 6071 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue- The Lunar New Year wa

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

【symfoware NATIVE】数据库应用

Symfoware  NATIVE系 [共通]应用程序开发环境开发语言            操作数据库方法  C                   SQL嵌套  C++                 ODBC  C#                  .NET Framework  COBOL               SQL嵌套.ODBC..NET Framework  Java                Java  Visual Basic        ODBC  Visual

【开源.NET】 分享一个前后端分离的轻量级内容管理框架

开发框架要考虑的面太多了:安全.稳定.性能.效率.扩展.整洁,还要经得起实践的考验,从零开发一个可用的框架,是很耗时费神的工作.网上很多开源的框架,为何还要自己开发?我是基于以下两点: 没找到合适的:安全.稳定.简单.易用.高效.免费: 想成为架构师: 于是就自己动手,参考网上开源的项目和借鉴网友的设计思路(特别是萧秦系列博文),结合自己的实践,开发了一个简单.易用.高效的的框架,虽然不完善,但也能解决现实中的问题.不过随着见识增广,发现没负责过千万级别的项目难以成为架构师,也不可能开发出一个完