UVA 10041 Vito's Family (中位数)
Problem C: Vito's family |
Background
The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them.
Problem
Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem.
Input
The input consists of several test cases. The first line contains the number of test cases.
For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers) where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number.
Output
For each test case your program must write the minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers s i and s j is d ij = | s i - s j |.
Sample Input
- 2
- 2 2 4
- 3 2 4 6
Sample Output
- 2
- 4
题意:Vito有r个邻居。。在一条街道上。每个邻居有一个位置。 要求出Vito住的一个地方使得他到所有邻居距离总和最短。
思路:求中位数。。其实也不用求到中位数。。只要把所有邻居从小到大排序,分成两边。取最中间数就可以了。如果是偶数。就两个随便取一个就可以了。。具体原因自己想。
代码:
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <algorithm>
- using namespace std;
- int t, r, s[505], mid, sum;
- int main() {
- scanf("%d", &t);
- while (t --) {
- sum = 0;
- scanf("%d", &r);
- for (int i = 0; i < r; i ++)
- scanf("%d", &s[i]);
- sort(s, s + r);
- mid = s[r / 2];
- for (int i = 0; i < r ; i ++) {
- sum += abs(s[i] - mid);
- }
- printf("%d\n", sum);
- }
- return 0;
- }
UVA 10041 Vito's Family (中位数)的更多相关文章
- UVa 10041 - Vito's Family
题目大意:给出一些点,找到一个位置使这个位置到所有的点的距离的和最短. 很明显,排序,找中位数.关于中位数:有n个从小到大的数,k=(n+1)/2,若n为奇数,k为中位数,若n为偶数,k为中间那两个数 ...
- uva 10041 Vito's Family_贪心
题意:给你n个房子的距离,问那个房子离别的房子的距离最近,并且输出与别的房子距离的总和 思路:排序一下,中间的房子离别房子距离必然是最少的. #include <iostream> #in ...
- UVA 10041 (13.08.25)
Problem C: Vito's family Background The world-known gangster Vito Deadstone is moving to New York. ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- Zerojudge解题经验交流
题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
随机推荐
- C语言中fgetc、fputc和getc、putc的区别是什么
看书的时候,发现了这四个函数,想知道他们的不同.结果上网查发现很多人说fgetc.fputc的f代表的是file,就是这两个函数是和文件有关的!但是一看他们的函数声明,如下图: 发现他们的参数里面都有 ...
- 使用rapid-framework自动生成struct2
在JavaWeb的开发中,对数据对象的操作不外乎增删改查,不同的数据对象,其action.service.model.jsp等都比较类似,如果手动去写这些代码,工作量大且非常繁琐.一个心高气傲的程序员 ...
- 非常出色的一款WinForm窗体重绘GUI类库源码
基本控件的演示 ScrollBar滚动条 各种圆形进度条 ProgressBar进度条 Mdi演示,仿谷歌浏览器 多种皮肤可供选择 一套专业级别的GUI控件,目前包含了窗体.进度条.滚动条以及MDI多 ...
- js-shortid:优雅简洁地实现短ID
短ID在实际运用中很广泛, 其中比较典型的运用就是短地址. 市面上肯定有不少开源的生成短ID库, 基于node.js的估计也不少. 鉴于本人已然是node.js的脑残粉(本职java开发), 很多业余 ...
- eclipse安装ermaster建模插件
下载ermaster.jar 放到plugins重启eclipse即可
- js判断选择时间不能小于当前时间的代码
判断选择时间不能小于当前时间的方法有很多,在本文为大家详细介绍下使用js是如何实现的,感兴趣的朋友可以尝试操作下 复制代码代码如下: var controldate; function checkD ...
- 超棒的阿里巴巴矢量图标库——支持IE6
Iconfont.cn 是由阿里巴巴UX部门推出的矢量图标管理网站,也是国内首家推广Webfont形式图标的平台.网站涵盖了1000多个常用图标并还在持续更新 中,Iconfont平台为用户提供在线图 ...
- Spark Streaming揭秘 Day6 关于SparkStreaming Job的一些思考
Spark Streaming揭秘 Day6 关于SparkStreaming Job的一些思考 Job是SparkStreaming的重要基础,今天让我们深入,进行一些思考. Job是什么? 首先, ...
- 四、mysql内置函数
.字符串函数 concat('a','b'); 字符串拼接函数 ,,"我是A我是B"): 从指定位置开始替换指定长度的指定数据(起步为1) lower() 转小写 upper() ...
- 如何查看windows操作系统当前使用的字符集
如何查看windows操作系统当前使用的字符集 Chcp 显示活动控制台代码页数量,或更改该控制台的活动控制台代码页.如果在没有参数的情况下使用,则 chcp 显示活动控制台代码页的数量. 语法 ch ...