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 ...
随机推荐
- spring中基础核心接口总结
spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...
- JavaScript推断E-mail地址是否合法
编写自己定义的JavaScript函数checkEmail(),在该函数中首先推断E-mail文本框是否为空,然后在应用正則表達式推断E-mail地址是否合法,假设不合法提示用户 <script ...
- 你应当知道的Java牛人
Java领域有非常多著名的人物,他们为Java社区编写框架.产品.工具或撰写书籍改变了Java编程的方式. 本文是<最受欢迎的8位Java牛人>的2.0版本号. PS:排名不分先后.本文的 ...
- 通知/代理/block 三者比对
通知 : “一对多”,”多对一” 传值 四个步骤: 1.发送通知2.创建监听者3.接收通知4.移除监听者 使用场景:1- 很多控制器都需要知道一个事件,应该用通知:2 - 相隔多层的两个控制器之 ...
- 在 Android 中 Intent 的概念及应用
一.显式Intent: startActivity(new Intent(MainActivity.this, 类名.class)); 二.隐式Intent: 1.在AndroidManiFest ...
- TexturePacker 介绍
TexturePacker这个词从字面来说就是Texture(纹理) + Packer(打包). 它是一款把若干资源图片拼接为一张大图的工具.它由一位叫做Andreas Loew的老外开发的 :). ...
- codeforces 559A(Gerald's Hexagon)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Gera ...
- Week 5a - Mouse input and more lists----learning notes
pyton 程序内容的颠倒,运用 [](列表) def reverse_string(s): """Returns the reversal of the given s ...
- a标签阻止跳转的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Oracle逐行累加求和
最近遇到一个比较常见的问题,每行记录需要累加求和.这些问题倒不是有多难,主要是在工作的过程中会经常遇到,特别是Oracle自带的一些函数也能够很好地解决这样一些通用的查询计算,在此记录一下. 问题描述 ...