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. Codeforces 459C Pashmak and Buses 机智数学题

    这个题目说的是有n个人,有k辆巴士,有m天,每天都要安排n个人坐巴士(可以有巴士为空),为了使得这n个人不会成为朋友,只要每两个人在这m天里坐的巴士至少一天不相同即可. 要你求是否有这样的安排方法,如 ...

  2. case...when...和decode——oracle

    1.decode函数: 用法:decode(条件,值1,翻译1,值2,翻译2,......,缺省值): 例子: ','失败','未知') from table t1;--改变字段的显示值 ,变量1,变 ...

  3. 新版本vue-cli3.x 无法热更新问题【转载】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_38644883/articl ...

  4. InstrumentationTextCase 测试

    <instrumentation        android:name="android.test.InstrumentationTestRunner"        an ...

  5. Community Cloud零基础学习(三)Partner Account

    本篇参考:http://salesforce.vidyard.com/watch/bLE3QNRSej2iasw9vvc6Tk http://salesforce.vidyard.com/watch/ ...

  6. 数据类型和C#关系对应

    sqlserver与c#中数据类型的对应关系///private string changetocsharptype(string type){string reval=string.empty;sw ...

  7. vue项目配置多入口多出口【转载】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/localhost_1314/article ...

  8. 用Python在00:00给微信好友发元旦祝福语

    2019年的元旦即将来临,这里用Python撸一串简单的代码来实现定点给微信里的所有小伙伴发祝福语 环境说明 Python版本: 不限 第三方库: itchat, schedule 注:所有祝福语来源 ...

  9. CodeForces - 446A DZY Loves Sequences(dp)

    题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...

  10. redis--主从复制(读写分离)

    应用程序对服务器大量的读写,服务器很可能会宕机,导致数据丢失.为了解决这一问题就有了主从复制. 作用: 1:防止数据丢失 2:提高系统的吞吐量 主从复制:从服务器复制主服务器中的数据. 读写分离:应用 ...