UVa 10041:vito ' s Family

Source: Internet
Author: User

Link

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

"Original question"

Background

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

Problem

Vito wants to minimize, distance to all of them and has blackmailed your to write a, solves his proble M.

Input

The input consists of several test cases. The contains the number of test cases.

For the all test case you are given the integer number of relatives R (0 < R <) and the street numbers (also I Ntegers)

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

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

Output

For each test case your must write the minimal sum of distances from the optimal Vito ' Elatives. The distance between two street numbers SI and SJ are dij= |si-sj|.

Sample Input

2
2 2 4 3 2 4 6 

Sample Output

2
4

"The main effect of the topic"

A triad boss to move to a New York street, he has a lot of relatives and friends on that street, to find a place, so that the place to go to all relatives and friends of the total distance of the shortest.

"Analysis and Summary"

Naked to find the median on the OK ...

Code

/* 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) {//If it is odd, it must be the middle number 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{//If an even number, then is half the sum of the middle two 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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.