• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
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\n", a[0]);
    return 0;
}

 

posted on 2016-07-18 23:22  dwtfukgv  阅读(243)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3