zzu--2014年11月16日月賽 F題,zzu--2014日月

來源:互聯網
上載者:User

zzu--2014年11月16日月賽 F題,zzu--2014日月

Problem F: Difference RowTime Limit: 1 Sec  Memory Limit: 128 MB
Submit: 8  Solved: 3
[Submit][Status][Web Board]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 Input5100 -100 50 0 -50Sample Output100 -50 0 50 -100HINT

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 x[r+1] < y[r+1].



題目很水,,不知道為啥沒人做,可能是因為英文有點難懂吧。。


水題思路:只要將全部資料按從小到大排下序,然後交換第一個和最後一個就OK了!!


AC代碼:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){int n;int a[110];while(scanf("%d", &n)!=EOF){for(int i=0; i<n; i++){scanf("%d", &a[i]);}sort(a, a+n);for(int i=0; i<n-1; i++){if(i == 0) printf("%d ", a[n-1]);else printf("%d ", a[i]);}printf("%d\n", a[0]);}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.