A simple problem
来源:互联网 发布:国家大数据发展 编辑:程序博客网 时间:2024/05/09 14:28
A simple problem
题目描述
Dreamone has a lovely cat. The cat can comfort you if you are very dejected, she can also play with you if
you are very bored. So Dreamone loves her very much. Of course, you bet, she can also make you trouble if
she wants to be naughty. This time, she comes up with a simple problem for fun, just like this:
She has 2n toys. In her heart, she has used a number to stand for each toy, and for example, the 2n toys are
given like this:
A1、A2、A3、….A2n-1、A2n
She wants to divide them into two groups, where each group contains n toys. So we can use B1、
B2...Bn ,which are all from A1、 A2...A2n ,to stand for Group One ,use C1、 C2….Cn ,which are all from A1、
A2...A2n, to stand for Group Two. The cat wants to know the minimum value S for the expression below:
S=|B1-C1|+|B2-C2|+|B3-C3|+...+|Bn-Cn|
As we know Dreamone is studying MaJiang recently, so he has no time to solve the simply problem. But he
knows the 6th program contest of SWUST is on, so he turns to you for help. Can you help him?
输入
The first line of input will be a positive integer C indicating how many data sets will be included. Each of
the C data sets will contain two parts:The first part contains a number n(1<=n<=100000),and the second
parts contains 2n numbers ,which are A1、 A2、 A2n-1、 A2n (0<=Ai<=1000(1<=i<=2n)),represented 2n toys
输出
For each case, output the minimum value S for answer.
样例输入
2 1 1 3 2 1 1 1 2
样例输出
2 1
#include<iostream>
#include<algorithm>
using
namespace
std;
int
a[100005*2];
int
main()
{
int
n;
int
s1,s2,m,i;
cin>>n;
while
(n--)
{
s1=0;s2=0;
cin>>m;
for
(i=0;i<2*m;i++)
cin>>a[i];
sort(a,a+2*m);
for
(i=0;i<m;i++)
{
s1+=a[2*i];
s2+=a[2*i+1];
}
cout<<s2-s1<<endl;
}
return
0;
}
- A simple problem
- A simple problem
- HOJ A Simple Problem
- hdu A simple problem
- A simple problem
- HDU2522:A simple problem
- A Simple Math Problem
- A simple problem 2522
- A Simple Problem
- A simple problem
- A Simple Math Problem
- A Simple Math Problem
- CUSOJ1170--A Simple Problem
- A simple problem
- A simple problem
- 2522:A simple problem
- A simple water problem
- 2522A simple problem
- 动态解析EL表达式,调用JSF托管bean,输出返回结果
- 欧拉计划:题目3
- 黄淮学院CSDN高校俱乐部”沙漠掘金“小竞赛
- 怎样面向对象编程(上)
- 【gtk#】关于treeview数据绑定,选择及添加删除节点数据的备忘录
- A simple problem
- 常用的三层架构设计
- 黑马程序员----【javaSE基础】总结整理--泛型
- 7月12日
- 记来广州找实习1——还在找,还没找到。
- 使用maven命令行进行反向hibernate reverse hbm code 代码生成
- 海塞矩阵的定义
- Simple Breaking Waves in Maya
- 39. 面向对象的LotusScript(十一)之导出Excel(三)