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. C++ 对数组sizeof 和对数组元素sizeof

    这一段程序 下面这段程序很有看点://arr1 is an array of intsint *source=arr1;size_t sz=sizeof(arr1)/sizeof(*arr1);//n ...

  2. 51nod1212无向图最小生成树

    1212 无向图最小生成树 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Inpu ...

  3. Oracle 内核参数

    安装Oracle的时候,可以参考Oracle 的安装文档,来设置相关内核参数的值,但是有些参数的值还是需要根据我们自己的情况来进行调整.注:不同系统的参数不同,本篇针对linux. 一.Linux 系 ...

  4. Ubuntu系统启动时waiting for network

    最近在使用Ubuntu时启动经常会遇到等待网络配置, 每次等待时间都很长,要几分钟,于是在网上看看其他大牛怎么解决该问题. 有些解决方法中有提到删除 网卡硬件信息文件/etc/udev/rules.d ...

  5. 4、WPF应用程序的启动

    启动第一步: 启动第二步 启动第三步:

  6. go语言使用protobuf

    网上为什么充斥着大量几乎一模一样而且不正确的教程??? 妈的打开一个关于golang和protobuf的教程,无非都是protobuf多么多么牛逼,xml多么多么傻逼,然后就是怎么安装protobuf ...

  7. iOS 开发一年总结

    收获很多 1. 一个人包办从构思, 设计, 实现, 推广的全过程, 对自己的能力, 特别是能力范围有很大的提升. 以前在公司上班仅仅局限在实现的局域内, 现在在做自己的产品时, 在设计时的取舍, 对工 ...

  8. Unity3d之Socket UDP协议

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49201245 (一)Socket(套接字)UDP协议的特点 1.是基于无连接的协议,没 ...

  9. 【BZOJ 1491】 [NOI2007]社交网络

    Description Input Output 输出文件包括n 行,每行一个实数,精确到小数点后3 位.第i 行的实数表 示结点i 在社交网络中的重要程度. Sample Input 4 4 1 2 ...

  10. mvc权限,登陆,异常

    public class FilterOfPer : ActionFilterAttribute     {         public override void OnActionExecutin ...