CodeForces 347A Difference Row (水题)

题意:给定 n 个数,让你找出一个排列满足每个数相邻作差之和最大,并且要求字典序最小。

析:这个表达式很简单,就是把重新组合一下,就成了x1-xn,那么很简单,x1是最大的,xn是最小的,中间排序就好。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
const int maxn = 100 + 5;
int a[maxn];

int main(){
    int n;
    cin >> n;
    int ans = 0;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        //if(a[i] == i)  ++ans;
    }
    sort(a, a+n);
    printf("%d", a[n-1]);
    for(int i = 1; i < n-1; ++i)
        printf(" %d", a[i]);
    printf(" %d
", a[0]);
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5683100.html