B~A simple problem(13.7.12)
来源:互联网 发布:python安卓开发 编辑:程序博客网 时间:2024/05/16 11:11
B. A simple problem
Description
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?
Input
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
Output
For each case, output the minimum value S for answer.
1
1 3
2
Description
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?
Input
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
Output
For each case, output the minimum value S for answer.
Sample Input Sample Output
stdin
21
1 3
2
1 1 1 2
stdout
21
首先对数据进行从小到大的排列,然后算出每不同相邻两数的差的和即可
代码:
#include<iostream>#include<algorithm> using namespace std;int a[200000],s[100000];int main(){int c;cin>>c;int n;while(c--){int r=0;cin>>n;int d=2*n-1;for(int i=0;i<2*n;i++){cin>>a[i];}sort(a,a+2*n);while(d-1>=0){r=r+(a[d]-a[d-1]);d=d-2;}cout<<r<<endl;}return 0;}
- B~A simple problem(13.7.12)
- 14多校 B-A simple dynamic programming problem
- 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
- zedboard---------在主机Ubuntu下面建立和开发板(busybox文件系统)通信的网络文件系统
- typedef定义指针
- JAVA基础之理解JNI原理
- 突然决定要写博客了
- Flex中利用IFrame嵌入HTML网页后,出现菜单被遮挡的问题解决办法,包括IFrame别隐藏的问题
- B~A simple problem(13.7.12)
- 全排列算法
- 要学习的内容
- Linux 进程
- codeforce 237 C D
- 知识型企业如何进行人的管理
- linux----------CMake学习记录
- zoj 3529 博弈SG
- 3,少年白_一年前的我