UVa 10041 Vito's Family:中位数及快速选择

10041 - Vito's Family

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=982

Background

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

Problem

Vito wants to minimize the total distance to all 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 test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers)

where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number.

Output

For each test case your program must write the minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers si and sj is dij= |si-sj|.

Sample Input

2
2 2 4
3 2 4 6

Sample Output

2
4

排序求中位数即可。

优化:快速选择

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索number
, test
, to
, of
, The
中位数
uva lou s list、vito corleone、奔驰vito、vitotteng、奔驰vito商务车,以便于您获取更多的相关知识。

时间: 2024-12-01 04:50:16

UVa 10041 Vito's Family:中位数及快速选择的相关文章

uva 10041 - Vito&#039;s Family

点击打开链接uva 10041 题目意思: 有一个人住在纽约,现在他有r个亲戚,这个人经常会去所有的亲戚家,现在他想找到一个房子,使得每一次他去访问所有的亲戚时候走的路是最短的,输出这个最小值 解题思路: 1:思路:中位数+暴力 2:分析:假设值为这个人住的街道为x,那么根据公式有距离和为(abs(x-s[0])+abs(x-s[1])+......abs(s-s[r-1])) = abs(r*x-sum),sum 为s[0]+s[1]+......s[r-1]的和.所以要使得这个值最小,那么x

UVa 10041:Vito&#039;s Family

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=982 [原题] Background The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10057 A mid-summer night&#039;s dream

UVa 10057 A mid-summer night's dream. (二分&可以取哪些作为中位数?) 10057 - A mid-summer night's dream. Time limit: 30.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 This

UVA 之11300 - Spreading the Wealth

Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coi

UVA之11300 - Spreading the Wealth

 F. Spreading the Wealth  Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such th

uva 11300 - Spreading the Wealth

点击打开链接uva 11300 思路:数学分析+贪心 分析: 1 首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n.接下来我们用m来表示每人的最终的金币数 2 现在假设编号为i的人初始化为Ai枚金币,Xi表示第i个人给第i-1个人Xi枚金币,对于第一个人来说他是给第n个人. 3 根据第二点可以知道                                                                                              

在一个文件中有10G个整数,乱序排列,要求找出中位数

 题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存). 关于中位数:数据排序后,位置在最中间的数值.即将数据分成两部分,一部分大于该数值,一部分小于该数值.中位数的位置:当样本数为奇数时,中位数=(N+1)/2 ; 当样本数为偶数时,中位数为N/2与1+N/2的均值(那么10G个数的中位数,就第5G大的数与第5G+1大的数的均值了). 分析:明显是一道

Photoshop CS6快速选择工具的使用及抠图

呵呵,已经已经20多天没有写过博客了.前一段时间忙于工作的事情,而现在终于慢慢告一段落了. 现在工作的地方距离住的地方有大约一个半小时的路程.所以现在基本上一天的时间都是在路上了,由于刚到新公司一切都还没有完全熟悉起来.所以对于技术的研究也没有什么课深入的,同时现在基本上每天都是再熟悉公司的产品. 好了,不扯了.因为现在公司的产品使用的是oracle数据库,刚好咱也对这方面比较喜欢,呵呵,可能以后发表的更多的是和oracle数据库有关的哦! 进入正题,熟悉PS的都知道PS中的快速选择工具,那绝对