Sorted Adjacent Differences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have array of nn numbers a1,a2,…,ana1,a2,…,an.
Rearrange these numbers to satisfy |a1−a2|≤|a2−a3|≤…≤|an−1−an||a1−a2|≤|a2−a3|≤…≤|an−1−an|, where |x||x| denotes absolute value of xx. It's always possible to find such rearrangement.
Note that all numbers in aa are not necessarily different. In other words, some numbers of aa may be same.
You have to answer independent tt test cases.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains single integer nn (3≤n≤1053≤n≤105) — the length of array aa. It is guaranteed that the sum of values of nn over all test cases in the input does not exceed 105105.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
Output
For each test case, print the rearranged version of array aa which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
input
代码语言:javascript复制2
6
5 -2 4 8 6 5
4
8 1 4 2
output
代码语言:javascript复制5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a1−a2|=0≤|a2−a3|=1≤|a3−a4|=2≤|a4−a5|=2≤|a5−a6|=10|a1−a2|=0≤|a2−a3|=1≤|a3−a4|=2≤|a4−a5|=2≤|a5−a6|=10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a1−a2|=1≤|a2−a3|=2≤|a3−a4|=4|a1−a2|=1≤|a2−a3|=2≤|a3−a4|=4. There are other possible answers like "2 4 8 1".
思路:
直观感知一下,这只是div2B题,肯定不会很难,先排个序然后最小、最大、次最小、次最大放......你想想这样就和题目要求的反过来了,所以反着输出就好~
代码语言:javascript复制#include<bits/stdc .h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3) (x<<1) (ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res =sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x] =val;x =lb(x);}}//第x个加上val
ll t,a[100005];
int main()
{
cin>>t;
for(int i=1;i<=t;i )
{
ll x;
cin>>x;
for(int i=1;i<=x;i )cin>>a[i];
sort(a 1,a 1 x);
ll ans[100005];
int l=1,r=x;
for(int j=1;j<=x;j )
{
ans[j ]=a[l ],ans[j]=a[r--];
}
for(int j=x;j>=1;j--)
{
j==1?cout<<ans[j]<<endl:cout<<ans[j]<<" ";
}
}
return 0;
}