UVa 10041:Vito'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 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)

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

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

【题目大意】

一个黑社会老大要搬家到纽约的某一条街上, 他在那条街上有很多的亲戚朋友,要找到一个地方,使得这个地方走到所有亲戚朋友家的总距离最短。

【分析与总结】

赤裸裸的找中位数就OK了...

【代码】

/*
 * UVa: 10041   Vito's Family
 * Time: 0.024s
 * Author: D_Double
 *
 */
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define MAXN 510
using namespace std;
int arr[MAXN], n;  

void solve(){
    sort(arr, arr+n);
    if(n&1){ //如果是奇数,一定是正中间那个数
        int mid=arr[(n-1)>>1];
        int sum=0;
        for(int i=0; i<n; ++i)
            sum += abs(arr[i]-mid);
        printf("%d\n",sum);
    }
    else{ //如果是偶数,那么是中间两个之和的一半
        int mid=(arr[(n-2)>>1]+arr[n>>1])/2;
        int sum=0;
        for(int i=0; i<n; ++i)
            sum += abs(arr[i]-mid);
        printf("%d\n", sum);
    }
}  

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
            scanf("%d",&arr[i]);
        solve();
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, include
, number
, test
, arr
The
font family、family、modern family、family name、my family 英语作文,以便于您获取更多的相关知识。

时间: 2025-01-01 02:28:02

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:中位数及快速选择

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 ha

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

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 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener