AtCoder - 4371 Align(分类讨论)
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(分类讨论)的更多相关文章
- AtCoder Beginner Contest 173 E Multiplication 4 分类讨论 贪心
LINK:Multiplication 4 害怕别人不知道我有多菜 那就上张图: 赛时 太慌了 (急着AK 题目不难却暴露我的本性 根本不思考无脑写 wa了还一直停不下来的debug 至少被我发现了1 ...
- Codeforces 460D Little Victor and Set --分类讨论+构造
题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...
- BZOJ-1067 降雨量 线段树+分类讨论
这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...
- UVaLive 6862 Triples (数学+分类讨论)
题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...
- 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil
1177: [Apio2009]Oil Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1477 Solved: 589[Submit] Descri ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...
- 【cf789B】Masha and geometric depression(分类讨论/暴力)
B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...
- P2331 [SCOI2005]最大子矩阵 (动规:分类讨论状态)
题目链接:传送门 题目: 题目描述 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 输入输出格式 输入格式: 第一行为n,m,k( ...
- UVa 11722 Joining with Friend (几何概率 + 分类讨论)
题意:某两个人 A,B 要在一个地点见面,然后 A 到地点的时间区间是 [t1, t2],B 到地点的时间区间是 [s1, s2],他们出现的在这两个区间的每个时刻概率是相同的,并且他们约定一个到了地 ...
随机推荐
- 118-PHP调用带参数的成员方法
<?php class ren{ //定义人类 public function info($name,$age=3){ //定义有两个参数的成员方法 echo "我是{$name},年 ...
- 062-PHP函数按值传参,交换数值函数
<?php function swap($x,$y){ //定义交换数值函数 $temp=$x; $x=$y; $y=$temp; } $m=5; $n=15; echo "交换前:& ...
- 计算方法执行完的耗时 c#
Stopwatch watch = Stopwatch.StartNew(); //要执行的方法 test(); watch.Stop(); Console.WriteLine(string.Form ...
- Pyinstaller的安装及简单使用
(1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...
- 说说lock到底要锁谁?
波安搬... http://www.cnblogs.com/wolf-sun/p/4209521.html ---------------------------------------------- ...
- android 开发学习
androidSDK自带SQLite数据库,使用时继承父类(SQLiteOpenHelper). this表对象本身,理解为指向自身的指针:super(超类)表对象的父类,即指向父类的指针. Cont ...
- Web基础之Redis
Redis 什么是Redis?Redis是一个基于内存的非关系型数据库,简单来说就是一个可持久化的高速缓存. 常用场景: 缓存(数据查询,端链接,新闻内容,商品内容等等)--使用最多 聊天室的在线好友 ...
- java获取键盘事件
转 <script type="text/javascript" language=JavaScript charset="UTF-8"> docu ...
- 启动mysql遇到问题Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
在mysql的启动过程中有时会遇到下述错误 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 请问mys ...
- c++的符号表的肤浅认识
符号表是编译期产生的一个hash列表,随着可执行文件在一起 示例程序 int a = 10; int b; void foo(){ static int c=100; } int main(){ in ...