C. Challenging Cliffs

time limit per test

2 seconds

memory limit per test

256 megabytes

You are a game designer and want to make an obstacle course. The player will walk from left to right. You have n 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 i (1≤i<n) such that hi≤hi+1 where hi is the height of the i-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|, 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 t (1≤t≤100) --- the number of test cases. Then t test cases follow.

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

The second line of each test case contains n integers h1,...,hn (1≤hi≤109), where hi is the height of the i-th mountain.

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

Output

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

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

Example

Input

Copy

复制代码
2
4
4 2 1 2
2
3 1

Output

Copy

复制代码
2 4 1 2 
1 3

Note

In the first test case:

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

In the second test case:

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

解题说明:此题是一道模拟题,采用贪心算法, 首先从小到大排序后找到2个下标x,y 使得abs(h[x]-h[y])最小 ,可以发现 1 <= i < x 时 h[i] <= h[i+1] y <= i < n 时h[i] <= h[i+1] 所以把y到n中的数放前面 把1到x中的数放后面 当h[1]!=h[n]的时候 h[i] <= h[i+1]的i的个数最大为n-2 ,当h[1]==h[n]的时候 为n-1。

cpp 复制代码
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int t, n, i; 
	cin >> t;
	while (t--)
	{
		cin >> n; 
		int a[n], m = INT_MAX, j;
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		sort(a, a + n);
		for (i = 1; i < n; i++) 
		{
			if (a[i] - a[i - 1] < m)
			{
				m = a[i] - a[i - 1];
				j = i;
			}
		} 
		cout << a[j - 1] << " ";
		for (i = j + 1; i < n; i++)
		{
			cout << a[i] << " ";
		}
		for (i = 0; i < j - 1; i++)
		{
			cout << a[i] << " ";
		}
		cout << a[j] << endl;
	}
	return 0;
}
相关推荐
cici15874几秒前
基于MATLAB的GUI来对不同的(彩色或灰色)图像进行图像增强
开发语言·matlab
玄月初二丶8 分钟前
28. 找出字符串中第一个匹配项的下标
c语言·开发语言·数据结构·算法
小猪扒饭13 分钟前
C基础 12_day
c语言·笔记·学习·算法
奔跑吧邓邓子15 分钟前
从0到1学PHP(十):PHP 文件操作:读写与管理文件
开发语言·php·文件操作
沐风清扬1 小时前
Win10下python环境变量呼出微软应用商店
开发语言·python
屁股割了还要学2 小时前
【数据结构入门】时间、空间复杂度的计算
c语言·开发语言·数据结构·c++·算法
倒悬于世2 小时前
ThreadLocal详解
java·开发语言·jvm
啃火龙果的兔子3 小时前
快速搭建Java服务指南
java·开发语言
未来之窗软件服务3 小时前
智慧收银系统开发进销存库存统计,便利店、水果店、建材与家居行业的库存汇总管理—仙盟创梦IDE
java·开发语言·ide·进销存·仙盟创梦ide·东方仙盟·收银台
pusue_the_sun4 小时前
从零开始搞定类和对象(上)
开发语言·c++