C - Align


Time limit : 2sec / Memory limit : 1024MB

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is Ai. Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

  • 2≤N≤105
  • 1≤Ai≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1
:
AN

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.


Sample Input 1

Copy
5
6
8
1
2
3

Sample Output 1

Copy
21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3−8|+|8−1|+|1−6|+|6−2|=21. This is the maximum possible sum.


Sample Input 2

Copy
6
3
1
4
1
5
9

Sample Output 2

Copy
25

Sample Input 3

Copy
3
5
5
1

Sample Output 3

Copy
8

题解:分奇数和偶数讨论。当为奇数时,一定是中间的两个数在左右边(两种情况)使得结果最大。偶数同理,更好枚举,只有一种情况。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1e5+; int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif int n;
int a[maxn];
ll mx = ;
ll sum[maxn];
scanf("%d", &n);
for (int i = ; i < n; ++i) {
scanf("%d", a+i);
}
sort(a, a+n);
for (int i = ; i < n; ++i) {
if (!i) sum[i] = a[i];
if (i) sum[i] = sum[i-]+a[i];
}
if (n&) {
if (n == ) {
mx = max(abs(*a[]-a[]-a[]), abs(*a[]-a[]-a[])); //当n=3时,特殊讨论一下
} else {
//如果是选择靠左边的两个数作为两个端点,那么他们一定是小于它们相邻的数的。
//同理,如果是选择靠左边的两个数作为两个端点,那么他们一定是大于它们相邻的数的。
//将需要算两次的数*2
mx = max(abs(*(sum[n-]-sum[n/])-*(sum[n/-])-(a[n/]+a[n/-])), abs(*(sum[n-]-sum[n/+])-*(sum[n/-])+(a[n/]+a[n/+])));
}
} else {
mx = abs(*(sum[n-]-sum[n/])-*(sum[n/-])+abs(a[n/]-a[n/-]));
}
printf("%lld\n", mx);
#ifdef local
fclose(stdin);
#endif
return ;
}



Tenka 1 Computer Contest C-Align的更多相关文章

  1. Tenka1 Programmer Contest C - Align

    链接 Tenka1 Programmer Contest C - Align 给定一个序列,要求重新排列最大化\(\sum_{i=2}^{i=n} |a_i-a_{i-1}|\),\(n\leq 10 ...

  2. 用VsCode写Markdown

    Markdown 基本语法 段落 非常自然,一行文字就是一个段落. 比如: 这是一个段落 会被解释成: <p>这是一个段落.</p> 如果你需要另起一段,请在两个段落之间隔一个 ...

  3. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  4. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)

    题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...

  5. 2016 Multi-University Training Contest 1 G. Rigid Frameworks

    Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. 2016 Multi-University Training Contest 1 J.Subway

    Subway Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  7. Shanghai Regional Online Contest 1004

    Shanghai Regional Online Contest 1004 In the ACM International Collegiate Programming Contest, each ...

  8. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  9. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

随机推荐

  1. Python 字典删除元素clear、pop、popitem

    同其它python内建数据类型一样,字典dict也是有一些实用的操作方法.这里我们要说的是字典删除方法:clear().pop()和popitem(),这三种方法的作用不同,操作方法及返回值都不相同. ...

  2. linux 迁移项目ProtocolException

    背景:服务器跟换机房,虚拟机完整迁移项目,只修改ip和主机名 1.检查/etc/hosts 中ip 和主机名映射 2.检查网络端口是否有限制以及端口开放是否全了,检查ip有没有配对.RMI注册不上.

  3. java——类、对象、方法

    一.类 1.Java语言把一组对象中相同属性和方法抽象到一个Java源文件就形成了类. 一个java文件可以有多个类,但是每一个类都会生成一个class字节码文件. 如果class 前加public ...

  4. 汉诺塔I

    题目描述 对于传统的汉诺塔游戏我们做一个拓展,我们有从大到小放置的n个圆盘,开始时所有圆盘都放在左边的柱子上,按照汉诺塔游戏的要求我们要把所有的圆盘都移到右边的柱子上,请实现一个函数打印最优移动轨迹. ...

  5. linux在tomcat中指定jdk

    setclasspath.sh和catalina.sh中写入 export JAVA_HOME=/qbtapp/jdk-8u111-linux-i586/jdk1.8.0_111export JRE_ ...

  6. unsigned long long类型与long long类型

    最近做题的时候,经常遇到范围是2^63,取模2^64的这种题目.遇到这种限制条件时就要想到用unsigned long long类型. 可以简洁地声明为typedef unsigned long lo ...

  7. shell脚本定义输出字符颜色

    #-------------------定义输入颜色---------------------# RED='\033[1;31m' GREEN='\033[1;32m' YELLOW='\033[1; ...

  8. Python统计excel表格中文本的词频,生成词云图片

    import xlrd import jieba import pymysql import matplotlib.pylab as plt from wordcloud import WordClo ...

  9. idea【快捷键】

    ctrl+shift+a:全局搜索 IDEA 的操作和设置 shift+f6:重命名 ctrl+alt+m:提取方法 ctrl+alt+l:格式化代码 ctrl+alt+o:优化导入类和包 ctrl+ ...

  10. jq常用

    1.文本框.密码框.隐藏域.文本域(id换成对应的,value属性存在,才能利用attr(‘value’) 获取值,否则返回undefined): 1.1 获取—>$(“#text”).val( ...