forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1526A - Mean Inequality.cpp
78 lines (53 loc) · 1.59 KB
/
1526A - Mean Inequality.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
b is a permutation of a.
For every i from 1 to 2n, bi?bi-1+bi+12, where b0=b2n and b2n+1=b1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1=t=1000) — the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1=n=25).
The second line of each testcase contains 2n integers a1,a2,…,a2n (1=ai=109) — elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b1,b2,…b2n, for which the conditions from the statement are satisfied.
Example
inputCopy
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
outputCopy
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3,1,4,2,5,6] works, as it's a permutation of [1,2,3,4,5,6], and 3+42?1, 1+22?4, 4+52?2, 2+62?5, 5+32?6, 6+12?3.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int size = 2 * n;
vector <int> v(size);
for (int i = 0; i < size; i++) cin >> v[i];
sort(v.begin(), v.end());
int i = 0, j = size - 1;
while (i < j) {
cout << v[i] << " " << v[j] << " ";
i++;
j--;
}
cout << endl;
}
return 0;
}