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. Windows Azure 社区新闻综述(#74 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话.以下是本周的亮点. 文章.视频和博客文章 ·   Azure CDN:吸取的宝贵经验(10 月 ...

  2. 【Leetcode】Triangle

    给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...

  3. android 关于LCD背光调节渐变过程引起背光闪烁问题

    如果背光渐变过程会引起背光闪烁,可以采取以下任意一种方法修改:   方法1.减少调节级别时间 http://blog.csdn.net/sergeycao   默认的设计在关闭背光时会有灭屏动画,就是 ...

  4. hdu3416 Marriage Match IV【最短路+最大流】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  5. c 输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 (MD花了半天时间,思路不对害死人)

    输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 比如: 输入: 输出: 输入: 输出: #include<stdio.h> int main(void) { ...

  6. XML 学习之保存节点

    XmlDocumnet WriteTo 方法 说明: 保存xmldocument对象节点到xmlwriter(即xml文件写入流) 实例: public static void WriteXml( X ...

  7. Python网络编程——获取远程设备的IP地址

    有时需要把设备的主机名转换成对应的IP地址,下面是一个简单的操作. import socket def get_remote_machine_info(): # 定义get_remote_machin ...

  8. ELK架构浅析

    转自:http://blog.csdn.net/lively1982/article/details/50678657 ELK是Elasticsearch.Logstash.Kibana的简称,这三者 ...

  9. HBase 几点思考

    1. http://blog.csdn.net/yueyedeai/article/details/14648067 2. http://blog.csdn.net/pirateleo/article ...

  10. synapse socket总结三:心跳(Heartbeat)

    首先转载一篇关于心跳的博文解释: 所谓的心跳包就是客户端定时发送简单的信息给服务器端告诉它我还在而已.代码就是每隔几分钟发送一个固定信息给服务端,服务端收到后回复一个固定信息如果服务端几分钟内没有收到 ...