Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.

More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).

Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.

Input

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1, a2, ..., an (|ai| ≤ 1000).

Output

Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

Sample Input

Input

5
100 -100 50 0 -50

Output

100 -50 0 50 -100 

Hint

In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.

Sequence x1, x2, ... , xp is lexicographically smaller than sequence y1, y2, ... , yp if there exists an integer r(0 ≤ r < p) such that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.

题意:

给定一个序列,求使得(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn)最大且字典序最小的排列

化简式子,得到:x1-xn  即求出x1-xn最大的即可

然后按字典序最小的输出

代码:

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int a[130];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int x,y,res=-INF;
int xl,yl;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i]-a[j]>res){
x=a[i],y=a[j];
xl=i,yl=j;
res=a[i]-a[j];
}
if(a[j]-a[i]>res){
y=a[i],x=a[j];
xl=j,yl=i;
res=a[j]-a[i];
}
}
}
a[xl]=-INF,a[yl]=-INF;
sort(a,a+n);
cout<<x<<" ";
for(int i=0;i<n;i++){
if(a[i]!=-INF){
cout<<a[i]<<" ";
}
}
cout<<y<<endl;
}

codefroces Round #201.a--Difference Row的更多相关文章

  1. codefroces Round #201.B--Fixed Points

    B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. A. Difference Row

    A. Difference Row time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. codeforces round #201 Div2 A. Difference Row

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  4. Codeforces Round #201 (Div. 2)C,E

    数论: C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #201.C-Alice and Bob

    C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. codeforces A. Difference Row

    link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= ...

  7. codeforces A. Difference Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...

  8. codeforce Codeforces Round #201 (Div. 2)

    cf 上的一道好题:  首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是  所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...

  9. Codeforces Round #201 (Div. 2) - C. Alice and Bob

    题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...

随机推荐

  1. 2018.08.15【2018提高组】模拟A组 比赛总结

    总结 T1 这题我一看,哇!好简单啊! 直接建立每一个字母的映射就可以了. 我很快就打完了程序,跳到下一题. 等到比赛快结束时,我才发现了一个可怕的数据: 1 abcdefghijklmnopqrst ...

  2. Mac 安装 Homebrew

    为什么要在 MAC 上安装 Homebrew 它干什么用的呢?我们知道在 CentOS 和 Ubuntu 上都有自己的包管理工具,但是在 MAC 上却没有这样类似的管理工具. # CentOS $ y ...

  3. redis 学习(13)-- BitMap

    BitMap 什么是 BitMap BitMap,即位图,其实也就是 byte 数组,用二进制表示,只有 0 和 1 两个数字. 如图所示: 重要 API 命令 含义 getbit key offse ...

  4. JS基础_其他进制的数字(了解)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Clang调试CUDA代码

    Clang调试CUDA代码全过程 有空再进行编辑,最近有点忙,抱歉 使用的llvm4.0+Clang4.0的版本,依据的是上次发的llvm4.0和clang4.0源码安装的教程https://www. ...

  6. PYTHON的程序在LINUX后台运行

    1.nohup 命令 nohup nohup 命令 用途:LINUX命令用法,不挂断地运行命令. 语法:nohup Command [ Arg ... ] [ & ] 描述:nohup 命令运 ...

  7. LVM使用手册简化命令

    创建 hot_add    --查看新增的lun pvcreate /dev/sdb   --创建物理卷 pvcreate /dev/sdc   --创建物理卷 pvcreate /dev/sdd   ...

  8. VMWare中CentOS7 设置固定IP且能够访问外网

    最近搭建kubernetes集群环境时遇到一个问题,CentOS7在重启后IP发生变化导致集群中etcd服务无法启动后集群环境变得不可用,针对这种情况,必须要对CentOS7设置固定IP且可以访问外网 ...

  9. python-装饰(高阶函数)

    python-装饰(高阶函数) 高阶函数 1.把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码) 2.返回值 中包含函数名 高阶函数实现1的功能 def bar(): print(&qu ...

  10. AIX的shell脚本异常笔记

    一点点心得:1.set -x 运行时显示明细,前面加#则不显示2.空格要打好,如if [ -n "str" ]; then 可以,if[ -n "str" ]; ...