time limit per test
2 seconds
memory limit per test
256 megabytes
Sasha has an array aa of nn integers. He got bored and for all ii, jj (i<ji<j), he wrote down the minimum value of aiai and ajaj. He obtained a new array bb of size n⋅(n−1)2n⋅(n−1)2.
For example, if a=a= [2,3,5,12,3,5,1], he would write [min(2,3),min(2,5),min(2,1),min(3,5),min(3,1),min(5,1)min(2,3),min(2,5),min(2,1),min(3,5),min(3,1),min(5,1)] == [2,2,1,3,1,12,2,1,3,1,1].
Then, he randomly shuffled all the elements of the array bb.
Unfortunately, he forgot the array aa, and your task is to restore any possible array aa from which the array bb could have been obtained.
The elements of array aa should be in the range [−109,109][−109,109].
Input
The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases.
The first line of each test case contains a single integer nn (2≤n≤1032≤n≤103) — the length of array aa.
The second line of each test case contains n⋅(n−1)2n⋅(n−1)2 integers b1,b2,…,bn⋅(n−1)2b1,b2,…,bn⋅(n−1)2 (−109≤bi≤109−109≤bi≤109) — the elements of array bb.
It is guaranteed that the sum of nn over all tests does not exceed 103103 and for each array bb in the test, there exists an original array.
Output
For each test case, output any possible array aa of length nn.
Example
Input
Copy
5
3
1 3 1
2
10
4
7 5 3 5 3 3
5
2 2 2 2 2 2 2 2 2 2
5
3 0 0 -2 0 -2 0 0 -2 -2
Output
Copy
1 3 3 10 10 7 5 3 12 2 2 2 2 2 0 -2 0 3 5
Note
In the first sample, Sasha chose the array [1,3,3][1,3,3], then the array bb will look like [min(a1,a2)=1,min(a1,a3)=1,min(a2,a3)=3][min(a1,a2)=1,min(a1,a3)=1,min(a2,a3)=3], after shuffling its elements, the array can look like [1,3,1][1,3,1].
In the second sample, there is only one pair, so the array [10,10][10,10] is suitable. Another suitable array could be [15,10][15,10].
解题说明:此题是一道数学题,找规律可知,最小值出现的次数是n-1,因为分别把最小值放在开头和结尾,因为要取最小值所以在B组出现的次数一定是n-1。接着次小值一定出现n-2次,把次小值放在第二个位置完全成立。a的最小值就是出现n-1次的值,最大值就是出现1次或零次的值即大于等于b组的最大值。
#include <iostream>
#include <climits>
#include <bitset>
#include <vector>
#include <set>
#include <map>
using namespace std;#define N ((int)1e3 + 1)
int a[(int)N * (int)N];
void solve()
{map<int, int> mp;int n;scanf("%d", &n);int maxx = INT_MIN;for (int i = 1; i <= (n * (n - 1)) >> 1; ++i){scanf("%d", &a[i]);maxx = max(maxx, a[i]);++mp[a[i]];}vector<int> ans;ans.push_back(maxx);for (auto pos = --mp.end(); ; --pos) {auto i = *pos;int num = i.first;int cnt = i.second;int sz = (int)ans.size();ans.push_back(num);while (cnt != sz){cnt -= sz;++sz;ans.push_back(num);}if (pos == mp.begin()){break;}}for (auto i : ans) {printf("%d ", i);}putchar('\n');
}int main()
{int T;scanf("%d", &T);while (T--) {solve();}return 0;
}