码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 347A Difference Row (水题)

时间:2016-07-19 09:19:32      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定 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\n", a[0]);
    return 0;
}

 

CodeForces 347A Difference Row (水题)

标签:

原文地址:http://www.cnblogs.com/dwtfukgv/p/5683100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!