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 ...
随机推荐
- Microsoft 收购 Apiphany
StevenMartinMS 2013 年 10 月 23 日上午 10:00 今天,我高兴地宣布我们收购了业界领先的 API 管理交付平台 - Apiphany. 应用程序可扩展性已经不算什么新鲜事 ...
- HDU 1551 Cable master
题解:很显然的二分检索,在算法艺术上看过原题,不过这里要注意精度: #include <cstdio> int n,m; ]; bool test(double x){ ,i; ;i< ...
- 工程中.pch文件的作用 及使用方法
#ifdef __OBJC__ #define ABC 10#import "UIImage+Image.h"// 配置pch: buildSetting -> prefix ...
- ecshop标签大全 各个页面常用标签大全
先从index.php主页开始 页面关键字 {$keywords } 页面标题 {$page_title} 产品分类 父分类列表 {foreach from=$categories item=cat ...
- 飘逸的python - 一个最简单的服务器
python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行. from wsgiref.simple_server import make_server def simple_a ...
- 图像检索:一维直方图+EMD距离
EMD距离具体介绍已经在在这里已经给出. 思路:我们把一张图像的归一化的一维直方图作为signature的权值,也就是一般在比較两幅图像颜色直方图的EMD距离时,每一行的坐标一样,仅仅是权重值不一样. ...
- probing元素
https://msdn.microsoft.com/zh-cn/library/823z9h8w(v=vs.85).aspx 指定加载程序集时公共语言运行库要搜索的应用程序基子目录. <con ...
- 理解SQL SERVER中的分区表
转自:http://www.cnblogs.com/sienpower/archive/2011/12/31/2308741.html 简介 分区表是在SQL SERVER2005之后的版本引入的特性 ...
- STL中copy算法
STL中通过使用copy函数以提供一种方便的方式来输出容器中的元素.函数copy作为泛型算法的一部分,任何容器类型都可以使用.由于我们需要频繁的初始容器的元素,因此在继续讨论容器之前,先学习一下cop ...
- r语言之条件、循环语句
if条件语句:if (conditon) {expr1} else {expr2} > x<-1> if(x==1)+ {x<-"x=1"}else+ {x ...