算法题:UVA 662 Fast Food(dp)

Fast Food

The fastfood chain McBurger owns several restaurants along a highway. Recently, they have decided to build several depots along the highway, each one located at a restaurent and supplying several of the restaurants with the needed ingredients. Naturally, these depots should be placed so that the average distance between a restaurant and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.

To make this more precise, the management of McBurger has issued the following specification: You will be given the positions of n restaurants along the highway as n integers $d_1 < d_2 < dots < d_n$ (these are the distances measured from the company's headquarter, which happens to be at the same highway). Furthermore, a number $k (k le n)$ will be given, the number of depots to be built.

The k depots will be built at the locations of k different restaurants. Each restaurant will be assigned to the closest depot, from which it will then receive its supplies. To minimize shipping costs, the total distance sum, defined as

must be as small as possible.

Write a program that computes the positions of the k depots, such that the total distance sum is minimized.

Input

The input file contains several descriptions of fastfood chains. Each description starts with a line containing the two integers n and k. n and k will satisfy $1 le nle 200$, $1 le k le 30$, $k le n$. Following this will nlines containing one integer each, giving the positions di of the restaurants, ordered increasingly.

The input file will end with a case starting with n = k = 0. This case should not be processed.

Output

For each chain, first output the number of the chain. Then output an optimal placement of the depots as follows: for each depot output a line containing its position and the range of restaurants it serves. If there is more than one optimal solution, output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text.

Output a blank line after each test case.

Sample Input

6 3

5

6

12

19

20

27

0 0

Sample Output

Chain 1

Depot 1 at restaurant 2 serves restaurants 1 to 3

Depot 2 at restaurant 4 serves restaurants 4 to 5

Depot 3 at restaurant 6 serves restaurant 6

Total distance sum = 8

时间: 2024-10-30 21:28:54

算法题:UVA 662 Fast Food(dp)的相关文章

算法:uva 662 Fast Food (dp)

题意 一条直线马路上有n个饭店,各个坐标为di. 要在n个饭店中选择k个饭店用来建造停车 场.没有建停车场的饭店,只能使用附近最近的一个停车场. 问总距离最少的建造方案,并输出. 思路 先进行预处理,sum[i][j]表示在饭店i-j之间建一个停车场,i-j的所有饭店到停车场 的距离之和最小. 在饭店i-j之间,选择在(i+j)/2点建造是总距离最小的方案 f[i][j],表 示前i个饭店,建造j个停车场的最小总距离 那么, f[i][j] = min{ f[k-1][j] + sum[k][i

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:uva 10318

题目链接: 首先,可以确定每个格子只能选一次,因为选任何大于0的偶数次,等于没有效果 一样. 然后,就可以把这题理解成从r*c的矩阵中选择一些格子进行"点亮"操作,使得最终所 有格子都是"亮"的状态.那么,每个格子要么有点亮操作,要么没有,总共复杂度为2^25,显然必须 进行减枝. 假设从第一行第一列开始,从左往右,从上往下一次依次选择,对于当前所在位置( x, y),它已经不能影响到x-2以前的行数了,所以当到x行时,如果第x-2行没有全部点亮,则进行减枝 . 此

算法题:uva 1330

题目链接: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 以前做过一道一维的,这题只是变成了二维的,其他方法都一样.HDU 1506  Largest Rectangle in a Histogram   题解 代码1: #include<cstdio> #include<cs

算法题:poj 2541 Binary Witch(KMP水过,逆序转换)

链接: http://poj.org/problem?id=2541 分析与总结: 做这题估算了下复杂度,觉得无论KMP再怎么快,这题暴力也肯定要超时的. 想了很久也没想出个好办法,于是决定暴力之,但是TLE了....于是就放了几天.之后看了下discuss ,这题的正解应该是状态压缩dp,不过目前我还不懂,跪了. 之后百度发现也可以用KMP水过,虽然是因为数据水才过的,不过这种思路很巧妙,值得借鉴! 直接暴力是枚举字符串的后面13个的字母,然后再用KMP匹配,这样的话,就绪要枚举多次,分别是

时间复杂度-求教一个百度面试的算法题

问题描述 求教一个百度面试的算法题 一个有N个元素的一维数组(A[0],A[1], ..., A[n-1]),设计一个算法求解该数组最大子数组.(要求时间复杂度是O(n)) 解决方案 用动态规划http://www.cnblogs.com/xkfz007/archive/2012/05/17/2506299.html 解决方案二: http://www.ahathinking.com/archives/120.html 解决方案三: 哈,这道题啊,已经遇到好多次了,推荐一个很多人都在练习的网站,

从一道算法题说去2

今天的算法题是关于 字符串的最小编辑距离问题求解. 1. 什么是字符串编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,添加一个字符,删除一个字符. 例如将kitten一字转成sitting: a. sitten (k→s)  b. sittin (e→i)  c. sitting (→g)  俄罗斯科学家Vladimir Levenshtein在1965年提出

一个算法题,求答案啊啊啊啊

问题描述 一个算法题,求答案啊啊啊啊 白班 09:00-18:00 通班 09:00-21:00 每个人每个月通班数量必须等于早中班和中晚班数量之和 早中班 09:00-15:00 中晚班 15:00-21:00 假设:每月按照30计算. 排班规则: 1.每个人每个月固定休息6天连续上班天数不超过7天. 2.每天各班次上班的人数最低需求:8个白班5个通班1个早中班,2个中晚班. 3.每个月每个人的通班天数安排不超过8天. 4.每个人每个月早中班和中晚班的天数之和需要与通班天数相等. 5.每月最多

算法题:把阿拉伯金额转化为汉字表示的金额

n年没写算法题了,今天用了20分钟写了一个,要求如题,感觉算法有所退步,老了 using System; using System.Text; namespace money { class Program { static void Main(string[] args) { StringBuilder sb=new StringBuilder(); var strValue = Console.ReadLine(); var strlist = strValue.Split('.'); if