前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #633 (Div. 2) B Sorted Adjacent Differences(直观感知+排序插放)

Codeforces Round #633 (Div. 2) B Sorted Adjacent Differences(直观感知+排序插放)

作者头像
glm233
发布2020-09-28 14:28:56
5110
发布2020-09-28 14:28:56
举报

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;
    
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档