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.

Sample test(s)
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.

这题可以用三分,但是在三分判断条件的时候要注意,用循环限定次数比较好,因为double精度不够高。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 99999999
#define maxn 200060
#define eps 1e-10
int n;
double a[maxn],b[maxn],sum1[maxn],sum2[maxn],c[maxn]; double cal(double x)
{
int i,j,f;
double maxnum=0;
sum1[0]=sum2[0]=0;
for(i=1;i<=n;i++){
b[i]=a[i]-x;
sum1[i]=max(b[i],sum1[i-1]+b[i]);maxnum=max(maxnum,sum1[i]);
c[i]=x-a[i];
sum2[i]=max(c[i],sum2[i-1]+c[i]);maxnum=max(maxnum,sum2[i]);
}
return maxnum; } int main()
{
int m,i,j;
double l,r,m1,m2,minx,maxx;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%lf",&a[i]);
}
l=-10005;r=10005;
for(i=0;i<100;i++){
double d=(l+r)/2.0;
m1=d;
m2=(d+r)/2.0;
if(cal(m2)-cal(m1)>=0){
r=m2;
}
else l=m1;
}
printf("%.9f\n",cal(l));
}
return 0;
}

codeforces578C. Weakness and Poorness的更多相关文章

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

  3. Codeforces 578.C Weakness and Poorness

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

  4. 【CodeForces】578 C. Weakness and Poorness

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

  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] 578C Weakness and Poorness || 三分

    原题 题目定义了两个变量: poorness表示一个区间内和的绝对值. weakness表示一个所有区间最大的poornesss 题目要求你求一个x使得 a1 − x, a2 − x, ..., an ...

  7. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  8. CF578C:Weakness and Poorness——题解

    https://vjudge.net/problem/CodeForces-578C —————————————————————————— 题目大意:序列的数-x,求最大连续子序列和的绝对值的最小值. ...

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

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

随机推荐

  1. 最全的HashMap源码解析!

    HashMap源码解析 HashMap采用键值对形式的存储结构,每个key对应唯一的value,查询和修改的速度很快,能到到O(1)的平均复杂度.他是非线程安全的,且不能保证元素的存储顺序. 他的关系 ...

  2. Docker学习笔记之向服务器部署应用程序

    部署的应用仅仅是简单应用程序,使用的是node管理的web应用,具体我也不是很会,当然也可以配置tomcat服务器.这里主要是学习docker.需要客户机和服务机,其中服务机必须要为Linux操作系统 ...

  3. 【Linux】salt的cmd.script命令介绍

    salt是一个很棒的自动化运维工具之一,常用的有cmd.run,今天介绍的是cmd.script 其实一眼就能看出这个命令是执行脚本的命令 具体操作如下: 1.将/etc/salt/master中的 ...

  4. 基础Markdown语法

    Markdown语法 1.标题 //标题语法 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 一级标题 二级标题 三级标题 四级标题 ...

  5. 强制删除 Terminating 状态的pod

    [root@k8s-master coredns]# kubectl get podNAME                     READY   STATUS        RESTARTS   ...

  6. python--or 和 and 表达式

    or表达式: 两边为一真一假,返回真: 两边都为假,返回右边: 两边都为真,返回左边: and表达式: 两边为一真一假,返回假: 两边都为假,返回左边: 两边都为真,返回右边:

  7. printf函数输出格式总结

    printf函数格式 函数描述: printf("[格式化字符串]", [参数链表]); 函数声明: int printf(const char *format, ...) ; 输 ...

  8. 24V转3.3V芯片,同步降压调节器

    PW2312是一个高频,同步,整流,降压,开关模式转换器与内部功率MOSFET.它提供了一个非常紧凑的解决方案,以实现1.5A的峰值输出电流在广泛的输入电源范围内,具有良好的负载和线路调节. PW23 ...

  9. 阿里云RDS物理备份恢复到本地

    一:业务场景 验证阿里云备份文件可用性 二:恢复到本地过程中遇到的问题 1.修改密码报错 2.自定义函数不可用 三:恢复步骤 1.xtrabackup安装使用 请参考:https://www.cnbl ...

  10. uni-app开发经验分享十二: Android平台应用启动时读写手机存储、访问设备信息(如IMEI)等权限策略及提示信息

    Android平台从6.0(API23)开始系统对权限的管理更加严格,所有涉及敏感权限都需要用户授权允许才能获取.因此一些应用基础业务逻辑需要的权限会在应用启动时申请,并引导用户允许. 读写手机存储权 ...