F - Weakness and Poorness CodeForces - 578C

Problem Description

You are given a sequence of n integers a1, a2, …, an.

Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, …, an - x is as small as possible.

The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.

The poorness of a segment is defined as the absolute value of sum of the elements of segment.

Input

The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.

The second line contains n integers a1, a2, …, an (|ai| ≤ 10 000).

Output

Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, …, an - x. Your answer will be considered correct if its relative or absolute error doesn’t exceed 10 - 6.

Examples

Input
3
1 2 3
Output
1.000000000000000
Input
4
1 2 3 4
Output
2.000000000000000
Input
10
1 10 2 9 3 8 4 7 5 6
Output
4.500000000000000
Note

For the first case, the optimal value of x is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment “-1” or segment “1”. The poorness value (answer) equals to 1 in this case.

For the second sample the optimal value of x is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max poorness occurs on segment “-1.5 -0.5” or “0.5 1.5”. The poorness value (answer) equals to 2 in this case.

求减去x后的,连续字串和的绝对值的最小值。

题解

x偏大、偏小都会使连续字串的绝对值 deval 偏大,相当于deval 关于 x 的下凸函数,利用三分法查找到最低处的 x ,带入求出连续字串的最大绝对值

复杂度

求连续字串的最大字串的绝对值为 O(n)
三分 O(2log n )
O(2
nlogn ) == 1e7

算法

三分

代码

求连续字串和的绝对值的最大值

分正负讨论
正: 逐项求和,如果 sum 出现负值,则将 sum 清零, 继续寻找下一个连续和
负: 每一项都取负值, 则转化为求正值

整体代码

#include<bits\stdc++.h>
using namespace std;
#define maxn 200005
const double eps = 3e-12;
int n;
double a[maxn], l, r, midl, midr, ans, sum = 0;double weak(double x){ // 求连续字串和的绝对值的最大值 // 正数 sum = 0;ans = 0;for(int i = 1; i <= n; i++) {sum += (a[i] - x);ans = max(ans, sum);if(sum <= 0) sum = 0; // 如果sum>0则接下来的字串和将加上sum变得更大,若sum<0则和会减小 }// 负数 sum = 0;for(int i = 1; i <= n; i++) {sum += (x - a[i]); // 全部取负,与正数求法相同 ans = max(ans, sum);if(sum < 0) sum = 0; }return ans;
}int main() {freopen("test.in", "r", stdin);while(scanf("%d", &n) == 1) {l = 10000; r = -10000 ;for(int i = 1; i <= n; i++) {scanf("%lf", &a[i]);if(a[i] < l) l = a[i];if(a[i] > r) r = a[i];}while(r-l >= eps) {midl = l + (r-l)/3.0;midr = r - (r-l)/3.0;if(weak(midl) < weak(midr)) r = midr;else l = midl;}printf("%.15f\n", weak(l));}return 0;
}

F - Weakness and Poorness CodeForces - 578C相关推荐

  1. Weakness and Poorness

    Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. F. Bouncy Ball(Codeforces Round 859 (Div. 4))

    题目链接:Problem - F - Codeforceshttps://codeforces.com/contest/1807/problem/F 题意:给你一个n*m大小的网格,再给你一个起始点和 ...

  3. F. Strange Array(Codeforces Round #727 (Div. 2))(主席树)

    F. Strange Array 给定一个长度为nnn的数组aaa,1≤ai≤n1 \leq a_i \leq n1≤ai​≤n,对于每个aia_iai​,我们要找到一个l≤i,r≥il \leq i ...

  4. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

  5. Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...

  6. Codeforces Round #585 (Div. 2) F. Radio Stations 2-sat + 神仙建模

    传送门 文章目录 题意: 思路: 题意: 你现在有ppp种电台,有nnn对关系(x,y)(x,y)(x,y)代表xxx电台或yyy电台中至少有一个,mmm对关系(x,y)(x,y)(x,y)代表xxx ...

  7. Codeforces Round #675 (Div. 2) F. Boring Queries 区间lcm + 主席树

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的序列aaa,qqq个询问,每次询问[l,r][l,r][l,r]内的lcmlcmlcm是多少,对1e9+71e9+71e9+7取模. n ...

  8. Codeforces Round #726 (Div. 2) F. Figure Fixing 二分图 + 思维

    传送门 文章目录 题意: 思路 题意: 给你一张nnn个点mmm条边的图,每个点都有一个当前值aia_iai​,目标值bib_ibi​,每次可以选择一条边(i,j)(i,j)(i,j),将ai,aja ...

  9. Codeforces Round #727 (Div. 2) F. Strange Array 线段树 + 区间合并 + 排序优化

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的数组,对每个位置iii求一个最大价值,价值计算方式如下:选择一个包含iii的[l,r][l,r][l,r],让后将其拿出来排序,之后价值 ...

最新文章

  1. hdu 1043 Eight 搜索,哈希
  2. boost::multiprecision模块测试 cpp_dec_float_50 是否符合我们的 概念要求
  3. REST WebService与SOAP WebService的比较
  4. 物流系统车辆仓库定位的实现——基于RFID定位的实现方法
  5. 9. 工作区和暂存区
  6. 【转载】Delphi下实现鼠标自动点击器
  7. cad汉仪长仿宋体_长仿宋体字体下载 cad工程机械绘图工程制图国标字体下载
  8. 渗透测试基础总结(脚本小子)
  9. ACE ADMIN 大全
  10. 永中word页码怎么从第二页开始_用Word自动生成目录
  11. word中图片为嵌入式格式时显示不全_电脑中Word图片显示不全的六种处理方法
  12. 机器人无人车项目开发学习 上下位机软硬结合python jetson nano ros
  13. win10matlab2016启动卡,教你解决win10专业版开机卡死的方法
  14. 月均200wUP主活跃的B站涨粉密码是什么?
  15. Android and HTML5 开发手机应用
  16. 自动化运维之架构设计六要点
  17. 陌上花开,可缓缓归矣——2016年校招总结
  18. [转帖]房博士教你购房(六)
  19. 智慧树c语言石河子大学,2021知到网课 系统解剖学(石河子大学) 单元测试答案...
  20. Oracle回收站及flashback drop

热门文章

  1. 都在抢论文第一作者,怎么解决?
  2. 第十三篇:上下文无关语法 Context-Free Grammar
  3. Get 了滤镜、动画、AR 特效,速来炫出你的短视频开发特技!
  4. IntelliJ IDEA详细配置
  5. Kotlin学习笔记(五) 扩展函数 扩展属性
  6. java Timer定时器管理类
  7. ubuntu samba 安装
  8. Android 改变窗口标题栏的布局
  9. Linux RAR 安装和使用详细说明
  10. android平台水波效果 源码