题目链接: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. BZOJ-1968 COMMON 约数研究 数论+奇怪的姿势

    1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1513 Solved: 1154 [Submit] ...

  2. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...

  3. ACdream 1429 Rectangular Polygon

    Rectangular Polygon Time Limit: 1000MS   Memory Limit: 256000KB   64bit IO Format: %lld & %llu D ...

  4. 一个糟糕的Erlang练习题

    好吧,用的语法很糟糕...但是至少是做了练习. 题目 %The sequence of triangle numbers is generated by adding the natural numb ...

  5. 最小圆覆盖(Smallest Enclosing Discs)

    随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...

  6. Ubuntu学习总结-03 安装软件 & 技巧

    1 UBuntu 安装 Googole Chrome 首先下载软件 wget https://dl.google.com/linux/direct/google-chrome-stable_curre ...

  7. html5视频播放器

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. du: fts_read 失败: 无法分配内存

    今天在查看一个大的文件时突然报出一个du: fts_read 失败: 无法分配内存的错误. 用 ulimit -a 查看下 core file size (blocks, -c) 0 data seg ...

  9. Run UliPad 4.1 Under Windows 7 64bit and wxPython 3.0.2

    Abstract: UliPad that is developed by limodou is an excellent code editor. It works well with wxPyth ...

  10. 一个PHP写的简单webservice服务端+客户端

    首先是服务端,服务端有一个主要的class组成:apiServer.php <?php /** * apiServer.php * * webservice主类 * * @filename ap ...