C - Challenging Cliffs

You are a game designer and want to make an obstacle course. The player will walk from left to right. You have nn heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible. 

In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains ii (1≤i<n1≤i<n) such that hi≤hi+1hi≤hi+1 where hihi is the height of the ii-th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them. 

From all the arrangements that minimize |h1−hn||h1−hn|, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.

Input

The first line will contain a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then tt test cases follow.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of mountains.

The second line of each test case contains nn integers h1,…,hnh1,…,hn (1≤hi≤1091≤hi≤109), where hihi is the height of the ii-th mountain.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output nn integers — the given heights in an order that maximizes the difficulty score among all orders that minimize |h1−hn||h1−hn|.

If there are multiple orders that satisfy these requirements, you may output any.

Example

Input

2
4
4 2 1 2
2
3 1

Output

2 4 1 2 
1 3

Note

In the first test case:

The player begins at height 22, next going up to height 44 increasing the difficulty by 11. After that he will go down to height 11 and the difficulty doesn't change because he is going downhill. Finally the player will go up to height 22 and the difficulty will increase by 11. The absolute difference between the starting height and the end height is equal to 00 and it's minimal. The difficulty is maximal.

In the second test case:

The player begins at height 11, next going up to height 33 increasing the difficulty by 11. The absolute difference between the starting height and the end height is equal to 22 and it's minimal as they are the only heights. The difficulty is maximal.

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long int ll;
 
int a[200005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		int de = 1e9, d1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		if (n == 2)
		{
			cout << a[1] << " " << a[2] << endl;
		}
		else
		{
			for (int i = 1; i < n; i++)
			{
				int dex = a[i + 1] - a[i];
				if (dex < de)
				{
					de = dex;
					d1 = i;
				}
			}
			//cout << "d1=" << d1 << endl;
			for (int i = d1 + 1; i <= n; i++)
			{
				cout << a[i] << " ";
			}
			for (int i = 1; i <= d1; i++)
			{
				cout << a[i] << " ";
			}
			cout << endl;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值