A. Difference Row
2 seconds
256 megabytes
standard input
standard output
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.
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.
5
100 -100 50 0 -50
100 -50 0 50 -100
#include<stdio.h>
#include<stdlib.h>
int a[];
int comp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
} int main()
{
int n,i;
while(~scanf("%d",&n))
{
for(i = ;i < n;i ++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[]),comp);
printf("%d ",a[n-]);
for(i = ;i < n-;i ++)
printf("%d ",a[i]);
printf("%d\n",a[]);
}
return ;
}
A. Difference Row的更多相关文章
- codeforces A. Difference Row
link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= ...
- codeforces A. Difference Row 解题报告
题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...
- codeforces round #201 Div2 A. Difference Row
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- CodeForces 347A Difference Row (水题)
题意:给定 n 个数,让你找出一个排列满足每个数相邻作差之和最大,并且要求字典序最小. 析:这个表达式很简单,就是把重新组合一下,就成了x1-xn,那么很简单,x1是最大的,xn是最小的,中间排序就好 ...
- codeforces 347A - Difference Row
给你一个序列,让你求(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).值最大的一个序列,我们化简一下公式就会发现(x1 - x2) + (x2 - x3) + . ...
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
RGB 颜色空间转 HSI 颜色空间的matlab程序实现 2014.10.20之前的内容有误,这里依据wikipedia更新了算法内容. 算法以wiki为准 https://en.wikipedia ...
- OLAP vs OLTP: what makes the difference
OLAP vs OLTP: what makes the difference OLPT and OLAP are complementingtechnologies. You can't live ...
- Difference between 'SAME' and 'VALID' padding
Difference between 'SAME' and 'VALID' padding 'SAME' padding 和 'VALID' padding 的区别 If you like ascii ...
- iOS7 UITableView Row Height Estimation
This post is part of a daily series of posts introducing the most exciting new parts of iOS7 for dev ...
随机推荐
- javascript闭包的理解
闭包是Javascript的一个难点,但也是一个很重要的知识点. 1.首先我们要知道变量作用域链 变量的作用域分两种:全局变量和局部变量.没有定义到任何函数中的变量为全局变量,在函数中定义的变量为局部 ...
- bzoj1014:[JSOI2008]火星人prefix
思路:插入.修改操作是splay的模型,然后询问的话就可以二分答案,然后再用splay去判,关键就在于怎么去判断. 可以用字符串hash,splay每个节点维护一个hash域,然后就可以定义一个进制去 ...
- 学习笔记---C++析构函数心得
1.动态分配的对象的析构函数 class man{ public: man(){ cout<<"man begin"<<endl; }; ~man(){ c ...
- Xcode 5.1 更新后插件不能用
打开目录 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 右击选择"显示包内容" 找到"Inf ...
- SQL的经典操作——批量复制同行的其它列数据到其它列数据
看图说话比较直观: 对比复制前后的数据表: 使用SQL语句:UPDATE OR ROLLBACK Content SET YINBIAO = YINBIAO2, GESHU = GESHU2 WHER ...
- H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验
<input type="text" class="input-text" value="${detail.supportingname }&q ...
- linux压缩与解压缩 tar命令
#压缩tar -czvf ***.tar.gz filetar -cjvf ***.tar.bz2 file#解压缩tar -xzvf ***.tar.gz filetar -xjvf ***.ta ...
- 解决cocos2d 热更是连不上https服务器
最近苹果宣布所有上架应用都要用https,我们后端走的是同一个函数,导致Android也要连接https. 百度了下,网上说客户端不需要做什么特殊处理,因为cocos本身是支持https的.但是服务端 ...
- 将FAT32格式转换成NTFS格式命令
convert f: /fs:ntfs /x 可以不用格式化里面的数据
- 动态网页技术---JSP
JSP(全称JavaServer Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成HTML.XML或其他格式文档的Web网 ...