• codefroces Round #201.a--Difference Row


    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;
    }
  • 相关阅读:
    vagrant up报错 Warning: Authentication failure. Retrying...解决方案
    node读写Excel操作
    批量转换word为pdf
    分享7个shell脚本实例--shell脚本练习必备
    shell脚本实例,通向shell脚本大师的必经之路
    前端优化DNS预解析
    如何选择开源协议
    深入理解document.referrer的用法
    使用 WebRTC 构建简单的前端视频通讯
    深入理解WebRTC
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6556863.html
Copyright © 2020-2023  润新知