E. Weakness and Poorness
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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,使得a[i]-x的这段连续最大子序列和绝对值最小;

思路:三分,check的时候来回扫一遍

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
double a[N],v[N];
int n;
double equ(double x)
{
double ans=;
double sum=;
for(int i=;i<=n;i++)
{
sum+=(a[i]-x);
if(sum<)
sum=;
ans=max(ans,sum);
}
sum=;
for(int i=;i<=n;i++)
{
sum-=(a[i]-x);
if(sum<)
sum=;
ans=max(ans,sum);
}
return ans;
}
double ternarySearch(double l,double r)
{
for(int i=;i<=;i++)
{
double lll=(*l+r)/;
double rr=(l+*r)/;
double ans1=equ(lll);
double ans2=equ(rr);
if(ans1>ans2)
l=lll;
else
r=rr;
}
return l;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf",&a[i]);
double ans=ternarySearch(-,);
printf("%f\n",equ(ans));
return ;
}

Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E. Weakness and Poorness 三分的更多相关文章

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

    显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...

  2. 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 ...

  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...

  4. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game

    题目链接:http://codeforces.com/contest/578/problem/B 题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作. ...

  5. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】

    A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)

    D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值

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

  8. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B "Or" Game (贪心)

    首先应该保证二进制最高位尽量高,而位数最高的数乘x以后位数任然是最高的,所以一定一个数是连续k次乘x. 当出现多个最高位的相同的数就枚举一下,先预处理一下前缀后缀即可. #include<bit ...

  9. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...

随机推荐

  1. C#IIS网站应用程序池启动回收停止 .

    //添加应用程序池空间引用using System.DirectoryServices;using System.Text; using System.Text.RegularExpressions; ...

  2. {sharepoint} SetPermission

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  3. 您需要安装旧 Java SE 6 运行环境才能打开“Eclipse”。

    mac删除jdk: sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk 旧版本sdk地址: http://www.oracle. ...

  4. 【Android】eclipse打不开的解决办法和“Jar mismatch! Fix your dependencies”的解决

    JDK1.7能用,cmd下输入java,javac,java -version,javaw配置和环境都没问题的话,有可能是工作空间的问题,就是一般在D盘下的workspace..那个文件夹,删除了,再 ...

  5. ExcelUtil工具类

    import com.google.common.base.Strings;import com.jianwu.util.excel.annotation.ExcelAttribute;import ...

  6. 基础概念 之 Hadoop Family

    Hadoop家族的技术,网上资料多如牛毛,但是还是那句老话——好脑瓜不如烂笔头,看的再多也不如自己动手写一写. Hadoop是一个分布式系统,有两个关键组件——HDFS和MapReduce,HDFS负 ...

  7. vue 学习报错 Newline required at end of file but not found

    着不敢了,原因竟然是需要在js css等后面再加一行(空行) Newline required at end of file but not found 翻译:文件末尾需要换行符,但找不到 如下面两处 ...

  8. CH1401 兔子与兔子【字符串】【HASH】

    1401 兔子与兔子 0x10「基本数据结构」例题 描述 很久很久以前,森林里住着一群兔子.有一天,兔子们想要研究自己的 DNA 序列.我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DN ...

  9. talib 中文文档(九):# Volatility Indicator Functions 波动率指标函数

    Volatility Indicator Functions 波动率指标函数 ATR - Average True Range 函数名:ATR 名称:真实波动幅度均值 简介:真实波动幅度均值(ATR) ...

  10. 嵌入式linux内核制作、根文件系统制作

    嵌入式系统构成: 主要由bootloader.kernel.以及根文件系统三部分组成. 内核制作步骤: 制作嵌入式平台使用的linux内核制作方法与pc平台上的linux内核基本一致 1.清除原有配置 ...