C. Birthday (思维 构造数组)

C. Birthday

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≤n≤100) — the number of the children who came to the cowboy Vlad’s birthday.

The second line contains integers a1,a2,…,an (1≤ai≤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

inputCopy
5
2 1 1 3 2
outputCopy
1 2 3 2 1
inputCopy
3
30 10 20
outputCopy
10 20 30

Hint

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.




题意:

给出若干人的身高, 求出相邻两人身高差最小的排列方案

题解:

数据很小, 解法很多~ 打比赛时候脑子抽了
解法1, 最大的放中间, 降序一次一左一右放, 直接构造数组即可
解法2, 二分+贪心判断
在此采用了解法1


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 110;

int n, a[maxn];
int ans[maxn*2];
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a+1, a+n+1);
    ms(ans, 0);

    ans[n] = a[n]; //最大的放中间
    for(int i = 1, j = -1; i <= n; i++,j*=(-1)){
        ans[n+(i+1)/2*j] = a[n-i];
    }
    for(int i = 1; i <= 2*n; i++)
        if(ans[i]>0)
            cout << ans[i] << " ";
    cout << endl;

	return 0;
}


猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/87916950