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.

Example

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, 1and 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.

#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#define MAXN 200009
#define eps 1e-11 + 1e-12/2
typedef long long LL; using namespace std;
/*
要求a[i]减去某个数字x后的最大字段和的最小绝对值!
s(a[i]-x)是单调的,加上绝对值之后变成单谷函数,三分搜索
*/ int n;
double a[MAXN],tmp[MAXN];
double cal(double x)
{
for (int i = ; i < n; i++)
tmp[i] = a[i] - x;
double cur = , ans = ;
for (int i = ; i < n; i++)
{
cur = cur + tmp[i];
if (cur < )
cur = ;
ans = max(cur, ans);
}
cur = ;
for (int i = ; i < n; i++)
{
cur = cur - tmp[i];
if (cur < )
cur = ;
ans = max(cur, ans);
}
return ans;
}
int main()
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf", &a[i]);
double beg = -, end = ;
int time = ;
while (time--)
{
double ml = (beg + beg + end) / , mr = (end + end + beg) / ;
if (cal(ml) > cal(mr))
beg = ml;
else
end = mr;
}
printf("%.15lf\n", cal(beg));
return ;
}

Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)的更多相关文章

  1. codeforces 578c//Weakness and Poorness// Codeforces Round #320 (Div. 1)

    题意:一个数组arr,一个数字x,要使arr-x的最大子段最小,问该最小值. 三分x,复杂度logn,内层是最大子段的模板,只能用n复杂度的.因为是绝对值最大,正负各求一次,取大的.精度卡得不得了,要 ...

  2. codeforces 578c - weekness and poorness - 三分

    2017-08-27 17:24:07 writer:pprp 题意简述: • Codeforces 578C Weakness and poorness• 给定一个序列A• 一个区间的poornes ...

  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp

    C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E. Weakness and Poorness 三分

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

  5. Codeforces E. Weakness and Poorness(三分最大子列和)

    题目描述: E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  6. Codeforces 578.C Weakness and Poorness

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

  7. 【CodeForces】578 C. Weakness and Poorness

    [题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...

  8. 三分搜索-ZOJ LightBulb

    开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...

  9. hdu 4355 Party All the Time(三分搜索)

    Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toget ...

随机推荐

  1. Redis集群创建和配置

    1.检查GCC是否安装,可以看看版本号 gcc -v 安装命令:yum install gcc-c++ 2.安装Ruby和Rubygems 如果有网的话,则通过yum命令进行安装,自动将关联的依赖包全 ...

  2. 递推DP UVA 1424 Salesmen

    题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...

  3. VMWare虚拟网络编辑

    VMWare虚拟网络编辑记录. 点击"编辑->虚拟网络编辑器" 在网卡列表中选择"VMnet8"查看目前设置,点击右下角的更改设置进行网络修改. 备注:更 ...

  4. [译]Cookies Without Chocolate Chips

    Cookies Without Chocolate Chips In the HTTP sense, a cookie is a name with an associated value. A se ...

  5. Android基础TOP6_3:Gally和ImageSwitcher实现画廊

    结构: Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  6. NodeJS、NPM安装配置步骤

    安装NodeJS和NPM 1.Node JS 官网下载地址 https://nodejs.org/en/download/ 2.安装完后,使用cmd 命令输入两个命令,查看安装状态 node -v n ...

  7. [Windows Server 2008] MySQL单数据库迁移方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:MySQL ...

  8. MySql学习笔记(二) —— 正则表达式的使用

    前面介绍利用一些关键字搭配相应的SQL语句进行数据库查找过滤,但随着过滤条件的复杂性的增加,where 子句本身的复杂性也会增加.这时我们就可以利用正则表达式来进行匹配查找. 1.基本字符匹配 ' o ...

  9. java基础学习之垃圾回收机制

    回收过程: 1.发现无用的对象 2.回收无用对象占用的内存的空间. 垃圾回收相关算法: 1.引用计数法 堆中每个对象都有一个引用计数.被引用一次,计数加一.被引用变量值变为null,则计数减一. 到计 ...

  10. 从整体上理解进程创建、可执行文件的加载和进程执行进程切换,重点理解分析fork、execve和进程切换

    学号后三位<168> 原创作品转载请注明出处https://github.com/mengning/linuxkernel/ 1.分析fork函数对应的内核处理过程sys_clone,理解 ...