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],他们出现的在这两个区间的每个时刻概率是相同的,并且他们约定一个到了地 ...
随机推荐
- .net core项目iis10上出现 HTTP 错误 500.19,错误代码:0x8007000d
文件权限更改,配置文件更改,IIS重装重启,各种办法都不管用,下面是解决办法: 看错误信息里:模块:IIS Web Core 打开IIS: 模块列表中是否缺少红框里的两个组件(我这是已经安装好的截图) ...
- VIJOS-P1282 佳佳的魔法照片 排序
Description 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从 ...
- ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建
ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建 集群规划: hostname NameNode DataNode JournalNode Re ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-text-width
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- spring源码 BeanFactory根接口
/* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Vers ...
- JAVAEE 和项目开发(第四课:HTTP的响应格式和响应状态码)
HTTP 协议之响应 响应格式的结构: 响应行(状态行):HTTP 版本.状态码.状态消息 响应头:消息报头,客户端使用的附加信息 空行:响应头和响应实体之间的,必须的. 响应实体:正文,服务器返回给 ...
- 如何把控好Essay写作结构
留学生在国内写过作文,但是对于essay写作到底了解多少呢?大家觉得essay写作太难是语言问题,但是大家要明白,老师对于内容的考察远重于对语言的考察.同学们的essay写作如果能做到言之有理,自圆其 ...
- 吴裕雄--天生自然TensorFlow2教程:高阶操作
import tensorflow as tf a = tf.random.normal([3, 3]) a mask = a > 0 mask # 为True元素,即>0的元素的索引 i ...
- Android群英传神兵利器读书笔记——第一章:程序员小窝——搭建高效的开发环境
1.1 搭建高效的开发环境之操作系统 1.2 搭建开发环境之高效配置 基本环境配置 基本开发工具 1.3 搭建程序员的博客平台 开发者为什么要写作 写作平台 第三方博客平台 自建博客平台 开发论坛 1 ...
- PTA 天梯赛 L1
L1-002 打印沙漏 细节:就是在 (i>j&&i+j<r+1) 这个区间里才有空格,然后就是 for 循环 for(r=1; ;r+=2) 条件不满足之后还会再 ...