Codeforces Round #201 (Div. 2) A. Difference Row
来源:互联网 发布:伊朗女孩 知乎 编辑:程序博客网 时间:2024/05/21 09:25
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 sequencea. 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.
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).
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.
5100 -100 50 0 -50
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 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.
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;#define M 10500bool cmp(int a,int b){ return a<b;}int pri[M];int main(){ int n,i; while(scanf("%d",&n)!=EOF){ for(i=0;i<n;i++){ scanf("%d",&pri[i]); } sort(pri,pri+n,cmp); printf("%d",pri[n-1]); for(i=1;i<n-1;i++){ printf(" %d",pri[i]); } printf(" %d\n",pri[0]); } return 0;}
- Codeforces Round #201 (Div. 2) A. Difference Row
- Codeforces Round #201 (Div. 2) 347A Difference Row(脑洞)
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- codeforces Round # 236(DIV 2)B. Trees in a Row
- Codeforces Round #236 (Div. 2)__Trees in a Row
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- codeforces 347A - Difference Row
- CodeForces 347 A. Difference Row
- Codeforces 347 A. Difference Row
- CodeForces 347A Difference Row
- CodeForces 347A Difference Row
- Codeforces Round #201 (Div. 2)A、B
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #174 (Div. 2) Problem A
- Codeforces Round #173 (Div. 2) A.Bit++
- Codeforces Round #181 (Div. 2) A题
- Codeforces Round #184 (Div. 2) A题
- Codeforces Round #185 (Div. 2)--A,B
- 【安博培训技术】Java4 异常处理 20130919
- 总结(9月21日)
- tomcat打开jsp文件中文乱码问题之解决
- 【安博培训技术】Java5 IO流 20130920
- GC日志分析
- Codeforces Round #201 (Div. 2) A. Difference Row
- 【安博培训技术】Java6 JDBC编程 20130921
- IOS图片压缩的类
- Codeforces Round #201 (Div. 2) B. Fixed Points
- 常用排序算法总结(二) ---- 插入排序,快速排序
- 工程师规范
- Codeforces Round #201 (Div. 2) C. Alice and Bob
- array and pointer
- 创业公司的切入点