• Codeforces 1131 C. Birthday-暴力 (Codeforces Round #541 (Div. 2))


    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 (2n1002≤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 (1ai1091≤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.

    题意就是构造序列,要求成一个环然后相邻两个差值尽量小。

    代码:

     1 //C
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 typedef long long ll;
     5 const int maxn=1e3+10;
     6 int a[maxn],b[maxn];
     7 
     8 int main()
     9 {
    10     int n;
    11     cin>>n;
    12     for(int i=1;i<=n;i++) 
    13         cin>>a[i];
    14     sort(a+1,a+1+n);
    15     int l=1,r=n;
    16     for(int i=1;i<=n;i++){
    17         if(i%2==1) b[l++]=a[i];
    18         else b[r--]=a[i];
    19     }
    20     for(int i=1;i<=n;i++)
    21         cout<<b[i]<<" ";
    22     cout<<endl;
    23 }
  • 相关阅读:
    消息队列之kafka
    注册中心ZooKeeper
    消息队列之RocketMQ集群部署
    消息队列之RocketMQ简介及单机部署
    消息队列之RabbitMQ
    消息队列简介
    debian10入门(切换root用户,更改网卡配置,及更新apt源配置)
    实体间的关系
    MySQL数据库的基础使用命令大全
    ReletiveLayout布局的一些常用属性
  • 原文地址:https://www.cnblogs.com/ZERO-/p/10426437.html
Copyright © 2020-2023  润新知