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 ...
随机推荐
- Collections集合工具类
一.Collection与Collections Collection 是所有单列集合的根接口 Collection 是操作集合的工具类 二.Collections中常见的方法:(大都是static方 ...
- java算法03 - 常用的8种排序算法
Java常用的八种排序算法: 插入排序 - 直接插入排序 每次将待排序的记录按照关键字的大小,插入到前面已经排好序的记录的适当位置.直到全部记录插入完成. 代码实现 /** * 直接插入排序 O(n^ ...
- Java冒泡法和二分法
最近去一家公司面试,手贱在人家CTO面前自告奋勇写了一把冒泡法,结果在交换数据的时候出了洋相,回家反思,写下如下代码,对自己算是一个鞭策,得到的教训是不要眼高手低,低调前行. package com. ...
- Python Day2 Learning record
一.python初始化模块 Python的强大之处在于他有非常丰富和强大的标准库和第三方库 ...
- Oracle组成介绍
Oracle Database 11g是一些特殊文件的集合,这些文件是用数据库配置助手创建的,然后用OEM Grid Control完成相关工作.这些数据库文件是通过一组共享内存进程来进行访问的,这组 ...
- socket.error: [Errno 99] Cannot assign requested address
方法一:python 命令行下运行 vi /etc/hosts 将127.0.1.1 那一行的名字改成你的(用 vi /etc/hostname 获取) 127.0.0.1 localhost 12 ...
- L358 World Book Day
World Book Day is celebrated by UNESCO and other related organisations every year on the 23rd of Apr ...
- JavaScript中Ajax的用法
XMLHttpRequest 对象的属性和方法: open(method,url,async) 规定请求的类型.URL 以及是否异步处理请求 send(string) 将请求发送到服务器. res ...
- ecside中<c:table>使用
<ec:table action="sjzc/tbWaterproject!list.do" items="objList" var="tbWa ...
- springboot单元测试自动回滚:@Transactional
2019-04-21 12:23:14.509 INFO 9384 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - St ...