Fast Food

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-08-27)

Description

 

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 nrestaurants along the highway as n integers  (these are the distances measured from the company's headquarter, which happens to be at the same highway). Furthermore, a number  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 knand k will satisfy . Following this will n lines 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

Miguel Revilla
2000-05-22
 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f; int a[],face[][],dp[][],cost[][]; int put(int k,int n)
{
if(k)
{
int up=face[k][n]+,mid=(up+n)/;
put(k-,face[k][n]);
if(up==n)
printf("Depot %d at restaurant %d serves restaurant %d\n",k,mid,n);
else
printf("Depot %d at restaurant %d serves restaurants %d to %d\n",k,mid,up,n);
}
return ;
} int main()
{
int n,K,mid,cas=;
int i,j,k;
while(scanf("%d %d",&n,&K)!=EOF)
{
memset(cost,,sizeof(cost));
if(n== && K==)
break;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=;i<=n;i++)
{
for(j=i;j<=n;j++)
{
mid=(i+j)/;
for(k=i;k<=j;k++)
{
cost[i][j]=cost[i][j]+abs(a[k]-a[mid]);
}
}
}
for(i=;i<=n;i++)
{
dp[][i]=cost[][i];
} for(i=;i<=K;i++)
{
for(j=i;j<=n;j++)
{
dp[i][j]=inf;
for(k=i-;k<j;k++)
{
if(dp[i][j]>(dp[i-][k]+cost[k+][j]))
dp[i][j]=dp[i-][k]+cost[k+][j],face[i][j]=k;
}
}
} printf("Chain %d\n",cas++);
put(K,n);
printf("Total distance sum = %d\n\n",dp[K][n]);
}
return ;
}

Problem W UVA 662 二十三 Fast Food的更多相关文章

  1. 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...

  2. 木块问题(The Blocks Problem,Uva 101)

    不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...

  3. WPF入门教程系列二十三——DataGrid示例(三)

    DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...

  4. Bootstrap <基础二十三>页面标题(Page Header)

    页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...

  5. Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十三】

    <Web 前端开发精华文章推荐>2014年第2期(总第23期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  6. iOS安全攻防(二十三):Objective-C代码混淆

    iOS安全攻防(二十三):Objective-C代码混淆 class-dump能够非常方便的导出程序头文件,不仅让攻击者了解了程序结构方便逆向,还让着急赶进度时写出的欠完好的程序给同行留下笑柄. 所以 ...

  7. WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇]

    原文:WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇] 在[第2篇]中,我们深入剖析了单调(PerCall)模式下WCF对服务实例生命周期的控制,现在我们来 ...

  8. Bootstrap入门(二十三)JS插件1:模态框

    Bootstrap入门(二十三)JS插件1:模态框 1.静态实例 2.动态实例 3.模态框的尺寸和效果 4.包含表单的模态框 模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能 ...

  9. JAVA之旅(二十三)——System,RunTime,Date,Calendar,Math的数学运算

    JAVA之旅(二十三)--System,RunTime,Date,Calendar,Math的数学运算 map实在是太难写了,整理得我都晕都转向了,以后看来需要开一个专题来讲这个了,现在我们来时来学习 ...

随机推荐

  1. 转:myeclipse 8.x 插件安装方法终极总结

    原文地址:http://shaomeng95.iteye.com/blog/945062 最近因为要指导新人顺便整理文档,懒得折腾eclipse,需要装的插件太多,于是乎装myeclipse 8.5吧 ...

  2. 深入Java核心 探秘Java垃圾回收机制(转自http://edu.21cn.com/java/g_189_859836-1.htm)

    垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...

  3. iBatis叙述

    1.添加Mybatis的配置文件conf.xml 在src目录下创建一个conf.xml文件,如下图所示: 2.定义表所对应的实体类 3.定义操作users表的sql映射文件userMapper.xm ...

  4. UINavigationController(转)

    UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...

  5. JS货币数字转换中文

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. selenium 右键另存为操作

    from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsbrowse ...

  7. 160912、工具类:spring+springmvc自定义编码转换

    一.自定义的类(注意其中的属性,web.xml中的配置就是根据这个类的) import org.springframework.web.filter.OncePerRequestFilter; imp ...

  8. JS和CSS的多浏览器兼容(2)

    2.Css的浏览器兼容性 方法一,根据不同的浏览器加载不同的css file <!DOCTYPE html>  <html> <head> <title> ...

  9. frameset子窗口获取父窗口失败原因?

    报错信息: arrow.html:44 Uncaught SecurityError: Blocked a frame with origin "null" from access ...

  10. java 面试每日一题2

    题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 注:如果想单独输出中文的个数和中文符号的个数,只需把isChinese()中的if语句修改 知识补充: java不像C中拥有s ...