B. Birthday

B. Birthday

http://codeforces.com/group/HD13CEfWEl/contest/239779/problem/B

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Cowboy Vlad has a birthday today! There are nn 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 11 to nn in a circle order, that is, for every ii child with number ii will stand next to the child with number i+1i+1, also the child with number 11 stands next to the child with number nn. 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 nn (2≤n≤1002≤n≤100) — the number of the children who came to the cowboy Vlad's birthday.

The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) denoting heights of every child.

Output

Print exactly nn 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

Copy

5
2 1 1 3 2

output

Copy

1 2 3 2 1

input

Copy

3
30 10 20

output

Copy

10 20 30

Note

In the first example, the discomfort of the circle is equal to 11, since the corresponding absolute differences are 11, 11, 11 and 00. Note, that sequences [2,3,2,1,1][2,3,2,1,1] and [3,2,1,1,2][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 2020, since the absolute difference of 1010 and 3030 is equal to 2020.

 

题意:

n个人,分别给出它们的高度。让他们围成一圈,使相邻两人高度差最小化。

输出一种情况即可。

思路:

那么一般单调递增的情况下最大值与最小值是不能排在一起的,那样的话这个方案的身高差就变大了,因此我们可以将相近的数分别放在其两边,这样处理的话就能够使其变得最小。

在这里我们用STL里面的双端队列去处理这个序列,先让最小的 a[1] 入队,

那么 a[2]、a[3] 分别在其两侧……依次类推

 

代码:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

deque<int>q;
int s[105];

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>s[i];
    sort(s+1,s+n+1);
    for(int i=1;i<=n;i++)
    {
        if(i&1)
            q.push_front(s[i]);
        else
            q.push_back(s[i]);
    }
    for(int i=0;i<n-1;i++)
        cout<<q[i]<<" ";
    cout<<q[n-1]<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include<string.h> #define N 10 typedef struct date { int year; int month; int day; }DATE; typedef struct student { int num; char name[20]; char sex; DATE birthday; float score; }STUDENT; void input(STUDENT *s); void output(STUDENT s); void outputarr(STUDENT s[ ], int n); int equal (STUDENT a, STUDENT b) ; int serach(STUDENT a[],int n,STUDENT x); int main() { STUDENT b[N]={ {10010,"Liyi",'M',2000,5,23,45}, {10020,"Lier",'M',2001,2,3,62.5}, {10030,"Lisan",'F',2000,10,14,92.5}, {10040,"Lisi",'F',2002,7,23,87}, {10050,"Liwu",'M',1999,8,6,78} }; STUDENT x; int n = 0; //printf("请输入查找的学生信息:\n"); input(&x); n = serach(b,5,x); outputarr(b,5); if(n>=0) { printf("查找成功,是第%d个学生,该学生信息如下:\n",n+1); output(b[n]); } else { printf("查无此人。\n"); } return 0; } void input(STUDENT *s) { //printf("请输入学生学号:"); scanf("%d",&(*s).num); //printf("请输入学生姓名:"); scanf("%s",(*s).name); //printf("请输入学生性别:"); scanf(" %c",&(*s).sex); //printf("请输入学生出生日期:"); scanf("%d%d%d",&(*s).birthday.year,& (*s).birthday.month, &(*s).birthday.day); //printf("请输入学生成绩:"); scanf("%f",&(*s).score); } void output(STUDENT s) { printf("学号:%d\t姓名:%s\t性别:%c\t", s.num,s.name,s.sex); printf("出生日期:%d-%d-%d\t", s.birthday.year,s.birthday.month, s.birthday.day); printf("成绩:%.1f\n", s.score); } void outputarr(STUDENT s[],int n) { } int equal (STUDENT a, STUDENT b) { } int serach(STUDENT a[],int n,STUDENT x) { } 完成此代码
最新发布
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值