Difference Row
来源:互联网 发布:长沙蓝狐网络官网 编辑:程序博客网 时间:2024/05/16 08:25
题目:
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.
解题思路:
观察题给公式,发现中间的数可以抵消,只剩下最两边的数,要想此值最多,只有将最大的数放在开头,最小的数放在末尾,就能得到的最大值,对于中间的数,
按从小到大排序便可以满足排列中lexicographically的最小
细节处理:
可以将输入的数字从小到大排序,再将头尾数交换
代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,x;
vector<int> a;
cin>>n;
for(i=0;i<n;i++)
{cin>>x; a.push_back(x);}
sort(a.begin(),a.end());
int t;
t=a[0]; a[0]=a[n-1]; a[n-1]=t;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
0 0
- Difference Row
- A. Difference Row
- codeforces 347A - Difference Row
- CodeForces 347 A. Difference Row
- Difference Row(347A)
- Codeforces 347 A. Difference Row
- CodeForces 347A Difference Row
- CodeForces 347A Difference Row
- Codeforces Round #201 (Div. 2) A. Difference Row
- numpy : the difference between row and column vector
- Codeforces Round #201 (Div. 2) 347A Difference Row(脑洞)
- what is the difference of select single and select up to one row in abap
- Row
- DB-difference
- Difference Equation
- Date Difference
- adjacent difference
- Expected Difference
- 深度学习原理部分数学公式
- 线程私有属性
- MySQL数据库基础
- ubuntu16.04下tensorflow-0.10(cuda7.5)安装
- org.jboss.tools
- Difference Row
- Scoket
- 2.9.PHP7.1 女神级教程-女神的私人信息 -【控制语句 循环】
- SpringMVC 基础教程 简单入门实例
- DBCP1.3连接泄露问题
- CodeForces 778B Bitwise Formula 解题报告
- jQuery dialog 学习笔记
- 线程池
- scp命令