Align AtCoder - 4371

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

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

6
3
1
4
1
5
9

Sample Output

25

Sample Input

3
5
5
1

Sample Output

8
题意:对于原序列进行排序,找到一种排序方法,使得相邻两个数字的差值的绝对值的和最大。
解题思路:将序列从小大到达排序,取后面一般的数列 减去 前面一半的数列 可以得到一个最大差值
  分奇数情况 和 偶数情况讨论 (叫前面一半的数列的元素为较小数, 后面一半数列的的元素为较大数)
奇数情况:假设序列的排序情况只有 大小大小大 或者 小大小大小 两种形式 较大数和较小数较差排列
      大小大小大情况 : 两端都少加了一次
      小大小大小情况 : 两端都少减了一次
偶数情况:大小大小 和 小大小大 情况相同
注意:数据范围!!!
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
using namespace std;
const int maxn = ;
long long n,ans;
long long num[maxn];
int main(){
scanf("%lld",&n);
for(int i = ; i <= n ; i++){
scanf("%lld",&num[i]);
}
sort(num + , num+ n + );
if(n&){
long long temp1 = ,tem1 = ,ans1 = ;
long long temp2 = ,tem2 = ,ans2 = ;
int t1 = n / + ;
for(int i = ;i <= t1 ; i++) temp1 += *num[i];
for(int i = t1 + ; i <= n ; i++) tem1 += *num[i];
ans1 = tem1 - temp1 + num[t1] + num[t1 - ];
int t2 = n/;
for(int i = ; i <= t2 ; i++) temp2 += *num[i];
for(int i = t2 + ; i <= n ; i++) tem2 += *num[i];
ans2 = tem2 - temp2 - num[t2 + ] - num[t2 + ];
ans = max(ans1,ans2);
}else{
long long t = n/;
long long tem = ,temp = ;
for(int i = ; i <= t ; i++) temp += *num[i];
for(int i = t + ; i <= n ; i++) tem += * num[i];
ans = tem - temp - num[t + ] + num[t];
}
printf("%lld\n",ans);
return ;
}

AC代码

一个从很久以前就开始做的梦。

 

AtCoder - 4371 Align(分类讨论)的更多相关文章

  1. AtCoder Beginner Contest 173 E Multiplication 4 分类讨论 贪心

    LINK:Multiplication 4 害怕别人不知道我有多菜 那就上张图: 赛时 太慌了 (急着AK 题目不难却暴露我的本性 根本不思考无脑写 wa了还一直停不下来的debug 至少被我发现了1 ...

  2. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  3. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

  4. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  5. 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil

    1177: [Apio2009]Oil Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1477  Solved: 589[Submit] Descri ...

  6. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp

    题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x  问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...

  7. 【cf789B】Masha and geometric depression(分类讨论/暴力)

    B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...

  8. P2331 [SCOI2005]最大子矩阵 (动规:分类讨论状态)

    题目链接:传送门 题目: 题目描述 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 输入输出格式 输入格式: 第一行为n,m,k( ...

  9. UVa 11722 Joining with Friend (几何概率 + 分类讨论)

    题意:某两个人 A,B 要在一个地点见面,然后 A 到地点的时间区间是 [t1, t2],B 到地点的时间区间是 [s1, s2],他们出现的在这两个区间的每个时刻概率是相同的,并且他们约定一个到了地 ...

随机推荐

  1. Django 数据库配置

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'books', #你的数据库名称 'USER': ' ...

  2. Perl 笔试题2 -- 统计单词频次

    Nvidia 2019 perl 笔试题 统计一个文件内单词的频次并排序 文本如下: "ALL happy families resemble one another; every unha ...

  3. SpringBoot安全认证Security

    一.基本环境搭建 父pom依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactI ...

  4. mnist数据集tensorflow实现

    TensorFlow——CNN实现MNIST手写体识别 2019年04月08日 21:46:19 星空Ice_ 阅读数 83   文章目录 TensorFlowCNN实现MNIST 1,数据集 2,回 ...

  5. AD走圆弧走线

    美式键盘: “shift  +  空格”

  6. PHP实现简单的双色球机选号码

    <?php header('Content-Type: text/html; charset=utf-8'); //PHP实现双色球机选号码 $red = range(1, 33);//初次设定 ...

  7. java课程之团队开发冲刺阶段2.10

    总结昨天任务: 1.已经完成修改APP等操作 遇到的困难: 1.在不同的Android版本中,APP的图标有出入 今天的任务: 最后一天,怀念一下,第二阶段结束了,课程也进入了尾声.

  8. Ubuntu下caffe:用自己的图片训练并测试AlexNet模型

    参考博客:https://blog.csdn.net/eereere/article/details/79118645#commentBox 目录 1.准备图片 2. 将 图片路径写入txt 参考 这 ...

  9. 读取word模板,填充数据后导出

    一.需求说明 定期生成word报告,报告中含有文本.表格.图表等元素,依次获取进行替换,保留原有样式,生成新的word文档 二.引入依赖 <dependency> <groupId& ...

  10. UVM实战[一]

    一个新的连载系列,将以一个实际的UVM环境代码讲解的使用.机制等,更新周期会比较长. 文件说明 分享的文件是我个人和同学在参加复微杯大学生电子设计大赛中所完成的设计.赛题来自数字命题AI赛道,有兴趣可 ...