2017上海金馬五校程序設計競賽 O:An Easy Problem

Problem O : An Easy Problem


 (Out of Contest)

Time Limit: 2 s

Description

Zhu Ge is a very clever boy. One day, he discovered 2*n numbers. He wanted to divide them into n groups, each group contains 2 integers, and minimize the sum of the absolute value of the difference of the numbers in each group.

The problem is too difficult to Zhu Ge, so he turned to you. He hopes you can calculate the minimum of the sum of absolute value of the difference among different division strategies.

 

Input

There are several test cases.
For each test case, there is an integer n (n < 10,000) at the first line. The second line contains 2*n integers. The input ends up with EOF.

 

Output

For each test case, output the minimum of sum.

 

Sample Input

3
10 3 4 5 1 6
5
64 5 63 63 23 63 54 64 3 54

 

Sample Output

7
42

題目意思就是給一個n,然後輸入n*2個數,分成n個組將每組的差的絕對值加起來,讓這個值最小,求出這個值。

就是按照大小順序排序,然後兩個兩個求出差值加起來就可以了。。

然後錯了。。

因爲加起來的值不是INT,LONG LONG INT DOUBLE 都可以,改一下救過了呢~_~

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    int n;
    int a[20001];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<2*n;i++)
        {
            scanf("%d",&a[i]);
            a[i]=fabs(a[i]);
        }
        sort(a,a+2*n);
        double sum=0;
        for(int i=1;i<=2*n-1;i=i+2)
        {
            sum=sum+a[i]-a[i-1];
        }
        printf("%.0lf\n",sum);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章