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. AX视图View中添加静态方法

    public static server str  EcoResCategoryName(){    DictView dv = new DictView(tableNum("ViewNam ...

  2. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  3. Flex-box入门---flex-grow, flex-shrink, flex-basis

    Block Elements and inline elements(块元素和行内元素) 在进入正题之前,我们先来简单总结一下传统的block元素和inline元素. HTML中的block元素显示在 ...

  4. linux-docker下安装禅道全部

    友情提示:按照步骤走,99%的人会安装成功,1%的人可以咨询度娘 64位电脑安装禅道,满足发送邮件功能 第一步: docker ps 查看docker中的容器是否有禅道(docker ps -a    ...

  5. jmeter的几种参数化使用方法

    场景:在进行jmeter的接口自动化测试脚本的编写中需要使用参数化,现将接触到的几种参数化方法整理如下: 第一种: 使用“用户自定义变量”的配置元件来进行变量定义 填入变量.值.和备注就可以在后续的接 ...

  6. idea 控制到不能输出中文

    解决办法:配置Tomcat的时候在VM options添加  -Dfile.encoding=UTF-8

  7. Javascript 数组相关操作

    数组排序问题: sort() arr.sort() 可以直接进行排序,但是排序的方式是按unicode 顺序而来,比如1,1000,200,这个顺序不是我们想要的结果: 所以有了另一种方法,针对num ...

  8. wpf 事件

    事件的本质是 系统消息  再winform中 消息被封装成了事件触发 这种就是 普通事件,特点就是 1对1,以及事件必须能访问到控件 才能绑定 在 wpf中 新概念  路由事件  ,路由区别是 不在是 ...

  9. asp.net 获取 repeater checkbox 值

    webform中获取repeat控件列表下的checkbox选中的值: 码农上代码: public static string getSelectedIDs(Repeater Rpt_) { stri ...

  10. 【笔记】.NET开发环境下使用PostgreSQL+Oracle_fdw 实现两个数据库之间数据交互操作(二)

    一 新的可视化工具 因为前文所提到的,看不到外部服务器和外部表的问题,我更换了可视化工具. 好用的新工具PostgreSQL Maestro! 当然如此好用的工具不是免费的,如果想免费使用还请自己去找 ...