Problem B: a simple problem
Time Limit: 1 sec memory limit: 128 MB
Submit: 67 solved: 26
[Submit] [Status] [Web
Board]
Descriptiondreamone 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 F Or 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?
Inputthe first line of input will be a positive integer c indicating how many data sets will be supported ded. 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 input2
1
1 3
2
1 1 1 2
Sample output2 1
To put it simply, the core of this question is:
S = | B1-C1 | + | B2-C2 | + | B3-C3 | +... + | bn-CN | the value of S is the minimum. the BC array is obtained from array A. Here, we only need to use sort to quickly sort array A and then one by one to throw it into the BC array. the table after sorting is as follows: A1 A2 A3 A4 · C1 B1 C2 B2 · en. This code is for reference only: I am not responsible for malicious copy!
# Include <iostream>
# Include <algorithm>
Using namespace STD;
Int A [220000];
Int main ()
{
Int N, I, j, M, K;
While (CIN> N)
{
For (INT I = 1; I <= N; I ++)
{
Cin> m;
M = m * 2;
For (Int J = 1; j <= m; j ++)
{
Cin> A [J];
}
Sort (a + 1, A + m + 1 );
Int sum = 0;
For (int K = 1; k <= m; k + = 2)
{
Sum + = A [k + 1]-A [k];
}
Cout <sum <Endl;
}
}
}