UVA 10041 (13.08.25)
| 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)的更多相关文章
- UVA 10340 (13.08.25)
Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limi ...
- 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 ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 156 (13.08.04)
Ananagrams Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...
- 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 ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
随机推荐
- AOP 面向切面编程、拦截器
AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是一种新的方法论, ...
- C/C++ 基础教程
自从做IOS后,就比较少用纯C++的方式写代码了,因为Obj-C的代码风格和C++的风格还是有一点区别的.怕自己忘记了C/C++的基础.整理了一些C/C++基础的网站,供大家学习C/C++ ...
- windows XP 安装pip
1.首先安装Python 2.添加环境变量 我的是 path = C:\Python27 3.下载setuptools 这里可参考这篇博文,附带资源的:http://blog.csdn.net/sud ...
- C语言数据结构----双向链表
概括:主要说明双向链表的基本概念和具体操作以及源代码. 一.基本概念 1.有了单链表以后我们可以把内存中小块的空间联系在一起,并且把每一个小块都存储上我们想要存储的数值.但是单链表只有一个next,我 ...
- Cocos2d-x 3.0 使用TinyXml 解析XML文件
在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...
- poj1144 Network【tarjan求割点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html ---by 墨染之樱花 [题目链接]http://poj.org/p ...
- asp.NET配置
添加用户 1.选择创建用户 2 可以使用网站管理工具来管理应用程序的所有安全设置.可以设置用户和密码(身份验证),可以创建角色(用户组),还可以创建权限(用于控制对应用程序各个部分的访问的规则). ...
- Backbone入门教程
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Node.js学习笔记3(快速入门)
一.开始使用Node.js编程 1.hello world 好了,让我们开始实现第一个 Node.js 程序吧.打开你常用的文本编辑器,在其中输入 ...
- Python网络编程——主机字节序和网络字节序之间的相互转换
If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...