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 (中位数)的更多相关文章

  1. UVa 10041 - Vito's Family

    题目大意:给出一些点,找到一个位置使这个位置到所有的点的距离的和最短. 很明显,排序,找中位数.关于中位数:有n个从小到大的数,k=(n+1)/2,若n为奇数,k为中位数,若n为偶数,k为中间那两个数 ...

  2. uva 10041 Vito's Family_贪心

    题意:给你n个房子的距离,问那个房子离别的房子的距离最近,并且输出与别的房子距离的总和 思路:排序一下,中间的房子离别房子距离必然是最少的. #include <iostream> #in ...

  3. UVA 10041 (13.08.25)

     Problem C: Vito's family  Background The world-known gangster Vito Deadstone is moving to New York. ...

  4. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

  7. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  8. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  9. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

随机推荐

  1. jobs

    fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的一.& 最经常被用到这个用在一个命令的最后,可以把这个命令放到 ...

  2. [原创] linux课堂-学习笔记-课程3.Linux目录结构介绍及内核与shell分析

    一.目录说明 1.1 bin 一般用户,可执行的系统内置命令 1.2 sbin 系统管理员,可执行的系统内置命令 1.3 boot 启动文件目录,启动有关的文件都保存在此 1.4 dev 设备管理文件 ...

  3. laravel--上传

    1.视图里面 2.控制器里面 //收集表单提交数据 $input = $request->all(); //查看是否有图片提交上来 if ($request->hasFile('title ...

  4. wampsever在win10中安装扩展掉坑

    1.必须要退出wampserver 2.php pecl + 3.wampserver 64 3.0.6

  5. WebApp之 apple-touch-icon

    在iPhone,iPad,iTouch的safari上可以使用添加到主屏按钮将网站添加到主屏幕上.apple-touch-icon是IOS设备的私有标签,如果设置了相应apple-touch-icon ...

  6. Mac OS X 10.10 Yosemite PHP 5.5.14 free type support fix

    通过将php将至5.4来勉强支持freetype扩展,不推荐此方法 after upgrading to new Mac OS X Yosemite, i noticed that free type ...

  7. HDFS导论(转)

    1.流式数据访问 HDFS的构建思想是这样的:一次写入,多次读取是最高效的访问模式.数据集通常有数据源生成或从数据源复制而来,接着长时间在此数据集上进行各类分析.每次分析都将设计数据集的大部分数据甚至 ...

  8. cocos2dx之Lua调用C++

    现在cocos2dx3.8自己封装了以前的toLua++,比以前更好用了. 先来看一下整体步骤: 1.编写一个.ini文件. 2,修改genbindings.py脚本. 3,执行genbindings ...

  9. 滤镜简单demo(转,供参考)

    NSURL *iamgeUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"default" ...

  10. dubbo zookeeper模型

    本文摘自dubbo的官方文档,原文请参见: http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm#UserGuide-zh-Zooke ...