题目链接
Cowboy Vlad has a birthday today! There are n children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.
Formally, let’s number children from 1 to n in a circle order, that is, for every i child with number i will stand next to the child with number i+1, also the child with number 1 stands next to the child with number n. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.
Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.
Input
The first line contains a single integer n (2 ≤ \leq ≤ n ≤ \leq ≤ 100) — the number of the children who came to the cowboy Vlad’s birthday.
The second line contains integers a1,a2,…,an (1 ≤ \leq ≤ ai ≤ \leq ≤ 1 0 9 10^9 109) denoting heights of every child.
Output
Print exactly n integers — heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.
If there are multiple possible answers, print any of them.
Examples
input |
---|
5 |
2 1 1 3 2 |
output |
1 2 3 2 1 |
input |
---|
3 |
30 10 20 |
output |
10 20 30 |
Note
In the first example, the discomfort of the circle is equal to 1, since the corresponding absolute differences are 1, 1, 1 and 0. Note, that sequences [2,3,2,1,1] and [3,2,1,1,2] form the same circles and differ only by the selection of the starting point.
In the second example, the discomfort of the circle is equal to 20, since the absolute difference of 10 and 30 is equal to 20.
题目大意
有n个人围成一个圈,每个人有自己一个高度,让你对这n个人进行重新的排列,使得满足任意两个人之间高度差的最大值最小。
解题思路
题目要求两个人之间的高度差尽可能的小,那么就要把数据差小的尽可能的安排到一起,自然就想到了首先要进行一个排序。
假设n=8,八个人的高度排序为
高度 | 2 | 3 | 4 | 6 | 9 | 11 | 14 | 16 |
---|---|---|---|---|---|---|---|---|
下标序列 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
对于第一个人,为了使高度差最小的话,只能让大于2的最小两个数排在2的左右两边,结果为:
高度 | 3 | 2 | 4 |
---|---|---|---|
下标序列 | 2 | 1 | 3 |
剩下的高度序列
高度 | 6 | 9 | 11 | 14 | 15 |
---|---|---|---|---|---|
下标序列 | 4 | 5 | 6 | 7 | 8 |
那么第四个数字6到底是放在左边还是右边呢?
让我们继续来看一下。
假设6放在在左边,那么6-3=3,如果6放在右边,那么6-4=2。因为3>2,所以把6放在右边? 显然,如果把6放在右边的话是错误的(因为6放在右边的话,9就只能放在左边了,9-3=6。当6放在左边的话,9放在右边,9-4=5。很明显6放在左边的时候,此时的最大值变的更小)。
高度 | 6 | 3 | 2 | 4 | 9 |
---|---|---|---|---|---|
下标序列 | 4 | 2 | 1 | 3 | 5 |
剩下的高度序列
高度 | 11 | 14 | 15 |
---|---|---|---|
下标序列 | 6 | 7 | 8 |
此时,我们就可以很容易的得出规律来,对高度序列排序之后,从n开始先输出下标序列为偶数的,然后在从1开始输出下标序列为奇数的值。
这道题还有另外一种思路,排序之后用双端队列进行模拟,双端队列参考这篇博客双端队列,在此不赘述。
AC代码
#include
#define INF 0x3f3f3f
using namespace std;
const int mod=1e9+7;
const int Max_N=1e3+7;
typedef pair<int,int>P;
typedef long long ll;
typedef unsigned long long ull;
int main(int argc, char const *argv[])
{
int n;
cin>>n;
int number[100+3];
for(int i=1;i<=n;i++)
{
cin>>number[i];
}
sort(number+1,number+n+1);
//代码是先输出下标为奇数的值,再输出下标为偶数的值
//因为是围成了一个圈,从哪一点开始输出并不影响。
for(int i=1;i<=n;i+=2)
{
if(i!=1)
printf(" ");
printf("%d", number[i]);
}
if(n%2==1)
n-=1;
for(int i=n;i>0;i-=2)
{
printf(" %d", number[i]);
}
return 0;
}
总结
切忌凭空想象,自己以为是这样,动手实践找规律才是真理。