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. 二十一、SAP中通过内表输出数据库中数据

    一.我们查看一个SCARR的一个数据库 二.数据库内容如下 三.我们写一个关于内表使用的代码,来显示这个数据库内容 四.输出如下

  2. Java singleton 单例

    饿汉式,instance在类加载化时完成初始化,线程安全 package cookie; public class SingletonAtOnce { private SingletonAtOnce( ...

  3. log4j 打印日志

    # Set log levels #     设置日志级别 log4j.rootLogger = INFO, DebugFile,Console, LogFile, ErrorFile ## Disa ...

  4. GDI+应用

    GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能;在C#.NET中,使用GDI+处理二维(2D)的图形和图像,使用Direct ...

  5. java如何连接Oracle数据库问题

    Oracle数据库纯属自学,不对请留言改正! 在学Oracle前相信已经大致知道mysql或sqlserver数据库,这个跟前面两个不大一样,你安装的时候让你输入一个密码,貌似是一个系统管理员密码,跟 ...

  6. Elasticsearch 使用集群 - 列出索引

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  7. OpenPose 开源库安装

    一.下载openpose下载openpose 使用Git直接下载openpose git clone https://github.com/CMU-Perceptual-Computing-Lab/o ...

  8. HDU - 5695 Gym Class (优先队列+拓扑排序)

    题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.在满足这个前提的情况下,将N个人排 ...

  9. UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)

    题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...

  10. UVALive 3634 数据结构模拟

    这题真是坑啊,题意不明,其实就是往桟里面压入空的set集合,所以之前的询问大小都是只有0,只有add的时候,才会产生新的占空间的集合 用stack和set直接进行模拟 #include <ios ...