2017年上海金马五校程序设计竞赛 O : An Easy Problem 贪心

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


题意,把这几个数分成两个两个一组,然后每组两个数的差值的绝对值之和最小的情况,其实一般看到这样子的情况基本都是贪心,这个题是个比较简单的贪心


就是先把他们排一下序,然后两两相减在相加,就好了,但是为啥那么多人WA呢,就是这个大坑,要用long long微笑



#include <iostream>
#include <algorithm>
#include <math.h>
long long X[100000];
using namespace std;
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<2*n;i++)
		{
			cin>>X[i];
		}
		sort(X,X+2*n);
		long long num=0;
		for(int i=0;i<n;i++)
		{
			num+=abs(X[2*i+1]-X[2*i]);
		}
		cout<<num<<endl;
	}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_我走路带风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值