码迷,mamicode.com
首页 > 其他好文 > 详细

codefroces Round #201.a--Difference Row

时间:2017-03-15 22:46:52      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:memory   should   rip   UI   code   span   pre   cccccc   strong   

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

标签:memory   should   rip   UI   code   span   pre   cccccc   strong   

原文地址:http://www.cnblogs.com/liuzhanshan/p/6556863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!