【IOI2000 - T5】邮局

@邮局@


@前言@ - 可忽略

Hi你们好这里是长期不写博客使得博客等级如今还是1级的渣渣(。・∀・)ノ゙
先简要介绍一下:这是一道有着约17年历史【比我还要老ヾ(。`Д´。)】的题目。但是,17年岁月如白驹过隙,一下子就流逝得无影无踪了。当初没钱建的邮局如今终于建好了
明明是一道IOI的题,做着难度却感觉和NOIP考过的乘积最大差不多……对对对这是一道区间DP的题
反正IOI2000DP题也挺多的,不缺这个邮局
好啦好啦,废话少说,我们赶紧进入正题,来看看这道题究竟如何吧!


@题目描述 - English@

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

@题目大意@

今有一条直路之村。公见示为一算轴,每村之位,以一算坐标以定之。同一位无两村。二位之相去,其成数坐标差者必直。
邮局将建于诸方,而不必为凡之村。一村及其所在之邮局有同之位。为师邮局,宜选择其位,使各村与近之邮局间之凡距之和小。
汝欲作一程式,及村之位与邮局之数,计每村及其近之邮局间可去者少与。

咳咳不开玩笑了ヾ(o◕∀◕)ノヾ

一条可以视为整数数轴的马路上有V个村庄,给定村庄的坐标(保证坐标不重复),两个村庄之间的距离被定义为坐标之差的绝对值。现在,我们要建P个邮局在一些(但并不是所有)的村庄里。邮局建立的坐标应该与村庄相同。为了服务大众【误】,邮局的位置应该正确地决定,使得每个村庄和最近的邮局的距离总和最小

@吐槽@ - 可忽略

啊你不会每个村庄都修一个邮局吗
啊为什么不改建村庄的位置
啊邮局为什么一定要建在村庄里
【抱怨ing】
算了算了,反正出题人总喜欢干些无聊的事,咱们正经地分析一下

@很骚的分析@ - 可忽略

还是从问题性质入手:很明显区间最优化问题,优先考虑贪心或动态规划。如果是动规的话,那就只能算是区间DP类型的了。既然如此,就按区间DP的思想去想想,可行吗?
定义状态: dp(i,j,k) d p ( i , j , k ) 表示在i~j的村庄中建k个邮局的最优值
显然最优子结构与无后效性是满足的(根据区间DP的性质),所以我们根据模板可以立即写出状·态·转·移·方·程·式

dp(i,j,k)=dp(i,u,v)+dp(u+1,j,kv+1)i<=u<j1<=v<k
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值