Tenka 1 Computer Contest C-Align
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
5
6
8
1
2
3
Sample Output 1
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
6
3
1
4
1
5
9
Sample Output 2
25
Sample Input 3
3
5
5
1
Sample Output 3
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的更多相关文章
- Tenka1 Programmer Contest C - Align
链接 Tenka1 Programmer Contest C - Align 给定一个序列,要求重新排列最大化\(\sum_{i=2}^{i=n} |a_i-a_{i-1}|\),\(n\leq 10 ...
- 用VsCode写Markdown
Markdown 基本语法 段落 非常自然,一行文字就是一个段落. 比如: 这是一个段落 会被解释成: <p>这是一个段落.</p> 如果你需要另起一段,请在两个段落之间隔一个 ...
- 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 ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)
题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...
- 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) ...
- 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 ...
- Shanghai Regional Online Contest 1004
Shanghai Regional Online Contest 1004 In the ACM International Collegiate Programming Contest, each ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
随机推荐
- erlang二进制
在Erlang中写处理二进制数据的代码是洋溢着幸福感的,它对于二进制强大的表现力甚至能让你忘掉了它种种不便,今天我们说说Erlang的二进制数据处理. Erlang中bit string代表无类型的内 ...
- grid-layout
<!-- 创建三个网格布局--> .wrapper { <!--创建一个网格布局 --> display: grid; <!--创建3列 且每列都等距 --> gr ...
- CPU-bound(计算密集型) 和I/O bound(I/O密集型) 区别 与应用
I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...
- [AutoMapper]反射自动注册AutoMapper Profile
AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...
- vue-cli 项目里屏幕自适应
很多同学可能在写h5的时候,也会遇到移动端如何控制屏幕自适应问题!在移动端网页开发中,我们可以用手机淘宝的flexible.那么在vue当中,也同样可以用!接下来就介绍下如何在vue-cli配置的项目 ...
- MyBatis通过Mapper动态代理来实现curd操作
MyBatis官方推荐使用mapper代理方法开发mapper接口,程序员不需要编写mapper实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或者map对象,保证dao的通用性 ...
- 字符串a-b
#include<iostream> #include<stdio.h> #include<algorithm> #include<cmath> #in ...
- Scrapy的使用
建立好项目以后,在项目文件内scrapy会搭好框架,我们只需要按照框架设置. 先定义Item 它是保存爬取到的数据的容器,其使用方法和python的字典类似,并且提供了额外保护机制来避免拼写错误 ...
- nginx 配置本地https(免费证书)
Linux系统下生成证书 生成秘钥key,运行: $ openssl genrsa -des3 -out server.key 20481会有两次要求输入密码,输入同一个即可 输入密码 然后你就获得了 ...
- 【温故知新】HTTP请求GET和POST的用法和区别
转自http://www.w3school.com.cn GET的使用 请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的: /test/demo_form.asp?name1=v ...