CF347A.Difference Row

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You want to arrange n integers _a_1, _a_2, ..., a__n 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 _x_1, _x_2, ..., x__n, where sequence x is a permutation of sequence a. The value of such an arrangement is (_x_1 - _x_2) + (_x_2 - _x_3) + ... + (x__n - 1 - x__n).

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.

你想将 $ n $ 个整数 $ a_1, a_2, \dots, a_n $ 按某种顺序排成一行。我们定义一个排列的“值”为所有相邻整数对之差的总和。

更准确地说,设某个排列为整数序列 $ x_1, x_2, \dots, x_n $,其中序列 $ x $ 是序列 $ a $ 的一个排列。该排列的值为

(x1−x2)+(x2−x3)+⋯+(xn−1−xn).(x_1 - x_2) + (x_2 - x_3) + \dots + (x_{n-1} - x_n).

求出可能的最大排列值;然后,输出字典序最小的、对应于该最大值的序列 $ x $。

输入格式

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers _a_1, _a_2, ..., a__n (|a__i| ≤ 1000).

输入的第一行包含一个整数 nn(2≤n≤1002 \leq n \leq 100)。第二行包含 nn 个用空格分隔的整数 a1,a2,…,ana_1, a_2, \dots, a_n(∣ai∣≤1000|a_i| \leq 1000)。

输出格式

Print the required sequence _x_1, _x_2, ..., x__n. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

输出所需的序列 x1, x2, ..., xnx_1,\,x_2,\,...,\,x_n。序列 xx 应为数组 aa 的字典序最小的排列,且该排列所对应的安排具有最大可能的值。

输入输出样例

  • 输入#1

    5
    100 -100 50 0 -50

    输出#1

    100 -50 0 50 -100

说明/提示

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 _x_1, _x_2, ... , x__p is lexicographically smaller than sequence _y_1, _y_2, ... , y__p if there exists an integer r (0 ≤ r < p) such that _x_1 = _y_1, _x_2 = _y_2, ... , x__r = y__r and x__r + 1 < y__r + 1.

在样例测试用例中,输出排列的值为 (100 − ( − 50)) + (( − 50) − 0) + (0 − 50) + (50 − ( − 100)) = 200(100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200。不存在其他排列具有更大的值;而在所有值为 200200 的排列中,输出排列是字典序最小的一个。

序列 x1, x2, …, xpx_1, x_2, \ldots, x_p 字典序小于序列 y1, y2, …, ypy_1, y_2, \ldots, y_p,当且仅当存在整数 rr(满足 0 ≤ r < p0 \le r < p),使得 x1 = y1, x2 = y2, …, xr = yrx_1 = y_1,\, x_2 = y_2,\, \ldots,\, x_r = y_r,且 xr+1 < yr+1x_{r+1} < y_{r+1}。

输入解题思路,AI测评打分。不知道怎么写?

首页