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.

题意:   给你一段序列a1, a2, ..., an

a1 - x, a2 - x, ..., an - x.    对于每一个x 都有ans=连续子序列的和的绝对值的最大值

输出min(ans)

题解:   贪心求出连续子序列的和的绝对值的最大值 o(n)处理

三分x (x为实数存在负数)  求min(ans)

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n;
double a[];
double fun(double x)
{
double sum1=0.0,sum2=0.0;
double max1=0.0,min1=100005.0;
for(int i=;i<=n;i++)
{
if((sum1+a[i]-x)<)
sum1=;
else
sum1=sum1+a[i]-x;
if((sum2+a[i]-x)>)
sum2=;
else
sum2=sum2+a[i]-x;
max1=max(max1,sum1);
min1=min(min1,sum2);
}
return max(abs(max1),abs(min1));
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf",&a[i]);
double l=-1e9,r=1e9,m1=0.0,m2=0.0;
for(int i=;i<;i++)
{
m1=l+(r-l)/3.0;
m2=r-(r-l)/3.0;
if(fun(m1)<fun(m2))
r=m2;
else
l=m1;
}
printf("%f\n",fun(l));
return ;
}

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

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

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

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

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

  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 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. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)

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

  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. 告诉你今年是哪个生肖年的java程序

    package com.swift; import java.util.Scanner; public class ChineseYear { public static void main(Stri ...

  2. dynamic routing between captual

    对于人脑 决策树形式 对于CNN 层级与层级间的传递 人在识别物体的时候会进行坐标框架的设置 CNN无法识别,只能通过大量训练 胶囊 :一个神经元集合,有一个活动的向量,来表示物体的各类信息,向量的长 ...

  3. 无屏幕和键盘配置树莓派WiFi和SSH

    原文转载:http://shumeipai.nxez.com/2017/09/13/raspberry-pi-network-configuration-before-boot.html 不算是什么新 ...

  4. vue.js 三(数据交互)isomorphic-fetch

    至于fetch的介绍,在这里就不多说了,官方和网络上的说明不少 之前使用jquery的Ajax朋友,可以尝试一下使用一下这个新的api 推荐使用isomorphic-fetch,兼容Node.js和浏 ...

  5. JS大小转化B KB MB GB的转化方法

    function conver(limit){ var size = ""; ){ //如果小于0.1KB转化成B size = limit.toFixed() + "B ...

  6. bootstrap-图片样式记录

    //三种形状<img src=”img/pic.png” alt=”图片” class=”img-rounded” /><img src=”img/pic.png” alt=”图片” ...

  7. NAND Flash和NOR Flash的比较

    目前Flash主要有两种NOR Flash和NADN Flash.NOR Flash的读取和我们常见的SDRAM的读取是一样,用户可以直接运行装载在NOR FLASH里面的代码,这样可以减少SRAM的 ...

  8. 动态规划:完全背包问题-HDU1114-Piggy-Bank

    解题心得: 1.这是一个完全背包问题的变形,题目要求是求在规定的重量下求价值最小,所以需要将d[0]=0关键的初始化 2.当不可能出现最小的价值时,d的状态并没有被改变,说明并没有放进去一个硬币. 题 ...

  9. 读书笔记jvm探秘之二: 对象创建

    对象是面向对象设计语言无法回避的东西,可见其重要性,JAVA的对象相较于C++来说,不算很复杂,但是我们看到一句话背后往往有很多东西值得探讨(NEW关键字). 对象如何被创建? 首先一句简单的NEW语 ...

  10. TCP/IP网络编程之多进程服务端(二)

    信号处理 本章接上一章TCP/IP网络编程之多进程服务端(一),在上一章中,我们介绍了进程的创建和销毁,以及如何销毁僵尸进程.前面我们讲过,waitpid是非阻塞等待子进程销毁的函数,但有一个不好的缺 ...