Problem - 1339B - Codeforces
输入
2
6
5 -2 4 8 6 5
4
8 1 4 2
输出
5 5 4 6 8 -2
1 2 4 8
给定长度为n的数组,将他们重新排序,使得两个相邻的元素之间差的绝对值最小
围绕着中位数插数
#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define x first
#define y second
#define fu(i,a,b) for(int i=a;i<=b;i ++ )
#define fd(i,a,b) for(int i=a;i>=b;i -- )
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<LL,LL> PLL;
//typedef __int128 i128;
typedef unsigned long long ULL;
const int N = 4e4+ 10,M = 1e5 + 10,INF = 0x3f3f3f3f ;
const int mod = 1e9;
const double eps = 1e-8;
int n,m,p[N];
inline void solve()
{
cin >> n;vector<int> a(n);
fu(i,0,n-1) cin >> a[i];
sort(a.begin(),a.end());
int m = n/2;
vector<int> b{a[m]}; // 一开始b中只有a[m]
// 从中间上取整的位置开始放数,然后向两边扩大
// 左边数的数量 大于等于右边的数的数量
fu(i,1,m)
{
b.push_back(a[m-i]);
if(m + i < n )
b.push_back(a[m + i ]) ;
}
fu(i,0,n-1)
cout << b[i]<<" \n"[i==n-1];
}
signed main()
{
// freopen("1.txt","w",stdout);
ios
int t=1;
cin>>t;
int now = 1;
while(t -- )
{
// cout<<"Case ";
// cout<<"Scenario #";
// cout<< now ++ <<": ";
// cout<< now ++ <<": \n";
solve();
}
return 0;
}