CF1095B Array Stabilization 题解
Content
有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\),现在需要从这些数中删除一个数,使得 \(\max\limits_{i=1}^na_i-\min\limits_{i=1}^na_i\) 的值最小。
数据范围:\(2\leqslant n\leqslant 10^5,1\leqslant a_i\leqslant 10^5\)。
Solution
先给这些数排个序,设最大数为 \(a_{\max}\),第二大数为 \(a'_{\max}\),第二小数为 \(a'_{\min}\),最小数为 \(a_{\min}\)。那么由于只能够删一个数,答案就是 \(\max\{a'_{\max}-a_{\min},a_{\max}-a'_{\min}\}\)。
Code
int n, a[100007];
int main() {
getint(n);
_for(i, 1, n) getint(a[i]);
sort(a + 1, a + n + 1);
int ans = min(a[n] - a[2], a[n - 1] - a[1]);
writeint(ans);
return 0;
}
CF1095B Array Stabilization 题解的更多相关文章
- Codeforces 1076G Array Game 题解
目录 题目大意 做法 代码 不想写昨天晚上cf的比赛题目所以来写题解摸摸鱼 题目大意 有一个在长度为\(k\)的正整数序列\(b\)上进行的游戏,一开始一个棋子放在位置\(1\),假如当前棋子的位置为 ...
- CF1445A Array Rearrangement 题解
Content 有 \(t\) 组询问,每组询问给定两个长度为 \(n\) 的数列 \(\{a_i\}_{i=1}^n,\{b_i\}_{i=1}^n\) 和一个整数 \(x\),求是否能够重新对两个 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #529 (Div. 3) 题解
生病康复中,心情很不好,下午回苏州. 刷了一套题散散心,Div 3,全部是 1 A,感觉比以前慢了好多好多啊. 这几天也整理了一下自己要做的事情,工作上要努力... ... 晚上还是要认认真真背英语的 ...
- [Leetcode] Search in Rotated Sorted Array 系列
Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- CodeForces Round #529 Div.3
http://codeforces.com/contest/1095 A. Repeating Cipher #include <bits/stdc++.h> using namespac ...
- LeetCode Can Place Flowers
原题链接在这里:https://leetcode.com/problems/can-place-flowers/description/ 题目: Suppose you have a long flo ...
- Codefoeces 734F. Anton and School 数学
Codefoeces 734F 题目大意: 给定两个正整数序列\(b,c\)构造一个正整数序列\(a\)使其满足 \[ \left\{ \begin{array}{} b_i=(a_i\text{ a ...
随机推荐
- N皇后问题解法
// // Created by Administrator on 2021/8/5. // #ifndef C__TEST01_NQUEENS_HPP #define C__TEST01_NQUEE ...
- spring boot的mybatis开启日志
logging: level: com: xxx: xxxx: xxxx: mapper: DEBUG logging.level.mapper对应的包名=DEBUG
- ABC201题解
因为学校的某些操作. 让最近两天的\(Atcoder\)都能打了,挺开心的. 想上次\(ABC\)看错题意,失败退场. ------------------------------ \(A\) 直接手 ...
- FMT 和 子集卷积
FMT 和 子集卷积 FMT 给定数列 $ a_{0\dots 2^{k}-1} $ 求 $ b $ 满足 $ b_{s} = \sum_{i\in s} a_i $ 实现方法很简单, for( i ...
- 洛谷 P7620 - CF1431J Zero-XOR Array(状压 dp)
洛谷题面传送门 首先显然题目等价于求有多少个长度 \(n-1\) 的序列 \(b\) 满足 \(a_i\le b_i\le a_{i+1}\),满足 \(b_1\oplus b_2\oplus\cdo ...
- Nginx编译安装相关参数
Nginx编译安装相关参数 Nginx插件安装 ------------------pcre------------------ cd /usr/local/source wget http://ww ...
- 关于SQL中Union和Join的用法
转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...
- 编程艺术第十六~第二十章:全排列/跳台阶/奇偶调序,及一致性Hash算法
目录(?)[+] 第十六~第二十章:全排列,跳台阶,奇偶排序,第一个只出现一次等问题 作者:July.2011.10.16.出处:http://blog.csdn.net/v_JULY_v. 引言 ...
- C语言中的指针与整数相加的值计算
以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数 3. 指针强制转换为另一个类型后(指针或者是整数) + 整数 测试例子: 1 struct AAA{ int a; char b[ ...
- A Child's History of England.25
It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...