题目链接:http://codeforces.com/problemset/problem/347/A

题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2, ..., xn ,使得 (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn)   的和最大。输出这个重排后的序列,不过这个序列在满足得到这个最大和的基础上字典序的排序是最小的。字典序排列即:

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 xr + 1 < yr + 1.

以测试案例来说: 100 -50 0 50 -100 和 100 -50 50 0 -100 都可以得到最大和200,但是前者的排列是比后者小的,因为第三个数0比50要小 !!!

一开始想得比较复杂,100!的排列?不可能的! 然后发现这个式子的特殊性 (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn) 。最终是可以变成 x1 - xn的(中间的全部消去了),那问题就变成如何得到 x1 - xn 的结果是最大的,很自然的想到是拿序列中最大的数减去最小的数就可以满足。至于中间的排列,为了符合字典序最小,那么必须第一个数是整个序列中次小的数,接着依次排到整个序列第二大的数就可以了。为什么这样可以?因为最终中间的那一堆数肯定是被消去的。

 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std; const int maxn = + ;
int a[maxn]; int main()
{
int i, n;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a+n);
printf("%d", a[n-]);
for (i = ; i < n-; i++)
printf(" %d", a[i]);
printf(" %d\n", a[]);
}
return ;
}

codeforces A. Difference Row 解题报告的更多相关文章

  1. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  2. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  3. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  4. codeforces B. Trees in a Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/402/B 题目意思:给出n个数和公差k,问如何调整使得ai + 1 - ai = k.(1 ≤ i < ...

  5. codeforces 475A.Bayan Bus 解题报告

    题目链接:http://codeforces.com/problemset/problem/475/A 题目意思:输入一个整数 k(0 ≤ k ≤ 34),表示participants的人数,需要在一 ...

  6. codeforces B. Simple Molecules 解题报告

    题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...

  7. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...

  8. 【LeetCode】500. Keyboard Row 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...

  9. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

随机推荐

  1. DHCP协议格式、DHCP服务搭建、DHCP协商交互过程入门学习

    相关学习资料 http://www.rfc-editor.org/rfc/rfc2131.txt http://baike.baidu.com/view/7992.htm?fromtitle=DHCP ...

  2. Laravel 5.3 中文文档翻译完成

    经过一个多月的紧张翻译和校对,翻译完成.以下是参与人员: Laravel 5.3 中文文档翻译完成 稿源:七星互联www . qixoo.com 文档地址在此:https://laravel-chin ...

  3. AndroidManifest File Features

    http://www.android-doc.com/guide/topics/manifest/manifest-intro.html The following sections describe ...

  4. Spring AOP 系列总括

    Spring有两大核心,IOC和AOP.IOC在Java Web项目中无时无刻不在使用,然而AOP用的比较少,尤其是对一些初级程序员,在架构师搭好的框架上开发应用代码,AOP几乎是透明的.然而,项目中 ...

  5. App接口简介

  6. Hacker's guide to Neural Networks

    Hacker's guide to Neural Networks Hi there, I'm a CS PhD student at Stanford. I've worked on Deep Le ...

  7. 修复UIImagePickerController偷换StatusBar颜色的问题

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:( ...

  8. spring mvc实现修改+删除

    1.在userController中添加修改的方法 a.首先点击修改,我们一般是到修改界面,并且上面有值,并且有提交按钮 b.修改后,提交到查看的页面 //进入修改界面 @RequestMapping ...

  9. Android Studio 设置不自动缩进匿名内部类

    Android Studio 会默认缩进匿名内部类代码,这让人感觉有些不大适应,可以使用下面的方法进行取消. 取消选中橙色框前的几个复选框即可.

  10. Ueditor的两种定制方式

    引言 UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 官网:http://ueditor ...