Problem C: Vito's family 

Background

The world-known gangster Vito Deadstone is moving to New York. He hasa very big family there, all of them living in Lamafia Avenue. Sincehe will visit all his relatives very often, he is trying to find ahouse close to them.

Problem

Vito wants to minimize the total distance toall 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 testcase you will be given the integer number of relatives r (0 < r < 500)and the street numbers (also integers) wherethey live (0 < si < 30000 ). Note that several relatives could live inthe same street number.

Output

For each test case your program must write the minimal sum ofdistances from the optimal Vito's house to each one of hisrelatives. 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

题意:

给主人公找个安家的位置, 使得与所有邻居距离的和最小~

思路:

找中位数, 然后所有的距离减去中位数即可

水题~

AC代码:

#include<stdio.h>
#include<algorithm> using namespace std; int R[555]; int main() {
int T;
scanf("%d", &T);
while(T--) {
int r;
scanf("%d", &r);
for(int i = 0; i < r; i++)
scanf("%d", &R[i]);
sort(R, R+r);
int mid = R[r/2];
int sum = 0;
for(int i = 0; i < r; i++) {
if(R[i] > mid)
sum += R[i] - mid;
else
sum += mid - R[i];
}
printf("%d\n", sum);
}
return 0;
}

UVA 10041 (13.08.25)的更多相关文章

  1. UVA 10340 (13.08.25)

    Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limi ...

  2. UVA 639 (13.08.25)

     Don't Get Rooked  In chess, the rook is a piece that can move any number of squaresvertically or ho ...

  3. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  4. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  5. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  6. UVA 156 (13.08.04)

     Ananagrams  Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...

  7. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  8. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  9. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

随机推荐

  1. HTML的标题样式

    标题样式1 <p> <span style=" text-align: center; padding-bottom: 6px; padding-left: 20px; p ...

  2. POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  3. apache添加fastcgi支持

    A,安装apache服务器和fastcgi模块支持(ubuntu测试) sudo apt-get install apache2 sudo apt-get install libapache2-mod ...

  4. Color the ball(树状数组+线段树+二分)

    Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  5. 道路软件质量:SourceMonitor

    有些事情必须这样做,不是幸福,但是,缓解疼痛,因为不.更痛苦--这是无奈. 夏中义 <文心独白> 1 简介 博客没有更新了一段时间,了阿里上市的成功之处:选择和坚持.事实上人生并没有绝对的 ...

  6. liquibase之快速入门

    第一步: 创建一个Changelog File: 这个database  Changelog file列举了数据库中所有的改变情况,该文件是以xml为基础的,下面是一个空的xml文件: <?xm ...

  7. 数据库基础(子查询练习、链接查询(join on 、union)及其练习)

    子查询练习一:查询销售部里的年龄大于35岁的人的所有信息 练习二:将haha表中部门的所有数字代码转换为bumen表中的字符串显示 练习三:将haha表中部门的所有数字代码转换为bumen表中的字符串 ...

  8. JavaSE学习总结第03天_Java基础语法2

      03.01 数据类型中补充的几个小问题 1:在定义Long或者Float类型变量的时候,要加L或者f.   整数默认是int类型,浮点数默认是double.   byte,short在定义的时候, ...

  9. BZOJ 2006: [NOI2010]超级钢琴( RMQ + 堆 )

    取最大的K个, 用堆和RMQ来加速... ----------------------------------------------------------------- #include<c ...

  10. RSA, ACS5.X 集成配置

    目的是RSA和ACS集成,ACS作为RADIUS服务器提供二次验证服务. ①配置RSA SecurID Token Servers   按照如下网址配置: http://www.cisco.com/c ...