A9068 | Difference Row
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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$ ).
输出格式
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.
输入输出样例
输入 #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}$ .
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}$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted