Weakness and Poorness

time limit per test

2 seconds

memory limit per test

256 megabytes

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

31 2 3

output

1.000000000000000

input

41 2 3 4

output

2.000000000000000

input

101 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越大,连续序列 最小和 的 绝对值 也越大;

所以,就会随x的递增,形成一个凹函数;

其次,求取连续序列 最大和 与连续序列最小和的 绝对值 :

    求连续序列最大和:

    设 b[i] = a[i] - x,sum(b[i]) = 连续序列中有 b[i] 的 序列最大和;

    若sum(b[j-1])<0,那么sum(b[j]) = max( b[j],b[j] + sum(b[j-1]) ),sum(b[j])必然等于b[j],这时sum(b[j])只加 自身 为最优;

    同理,求连续序列最小和,若sum(b[j-1]) > 0;sum(b[j]) = b[j];

至于卡精度的问题,我还是渣渣= =!,大神求教!

复杂度 O(n*log(n))

#include<cstdio>
#include<algorithm>
const double eps = 1e-6;
using namespace std;
int n;
double a[200005];
double b[200005];
double fabs(double x){if(x<0)return -x;return x;
}
double work(double x){for(int i = 0; i < n; i++)b[i] = a[i] - x;double sum = 0;double ans = 0;for(int i = 0; i < n; i++){sum += b[i];if(sum < 0)sum = 0;ans = max(ans,sum);}sum = 0;for(int i = 0; i < n; i++){sum += b[i];if(sum > 0)sum = 0;ans = max(fabs(sum),ans);}return ans;
}
int main(){scanf("%d",&n);for(int i = 0; i < n; i++){scanf("%lf",&a[i]);}double l = -10000;double r = 10000;for(int i = 0; i < 100; i++){double lmid = (l+r)/2;double rmid = (lmid+r)/2;if(work(lmid) > work(rmid))l = lmid;else r = rmid;}printf("%.6f\n",work(l));return 0;
}

转载于:https://www.cnblogs.com/ACMessi/p/4821141.html

Weakness and Poorness相关推荐

  1. F - Weakness and Poorness CodeForces - 578C

    F - Weakness and Poorness CodeForces - 578C Problem Description You are given a sequence of n intege ...

  2. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]

    数学 A - Raising Bacteria 分析:如果1 << k == x,那么放1个就可以了:否则还要加上差值的二进制的1的个数. /*********************** ...

  3. Common Weakness Enumeration (CWE) 2021 Lastest

    CWE 是社区开发的具有安全影响的常见软件和硬件漏洞类型列表."弱点"是软件或硬件实现.代码.设计或架构中的缺陷.故障.错误或其他错误,如果不加以解决,可能会导致系统.网络或硬件容 ...

  4. 2022-2028年中国老年旅游市场深度调研及开发战略研究报告

    [报告类型]产业研究 [报告价格]¥4500起 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了中国老年旅游行业市场行业相关概述.中国老年 ...

  5. 2022-2028年中国重卡行业投资分析及前景预测报告

    [报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了重卡行业相关概述.中国重卡行业运行环境.分析了中国重卡行业的现状.中 ...

  6. php slaveok_ZipArchive::open

    用户评论: [#1] ohcc at 163 dot com [2015-11-06 21:45:54] return values of ZipArchive::open() and their v ...

  7. OpenSSH7.0兼容性测试报告

    2019独角兽企业重金招聘Python工程师标准>>>   背景  2015年下,某省运营商综合网络管理系统. 按照安全管理要求,需对全系统主机的OpenSSH版本升级. 第一次测试 ...

  8. 每日英语:China's Labor Market Tightens

    Job cuts in China appear to be on the rise, dimming prospects for a labor market that has been a res ...

  9. jquery后学什么_我在训练营两年后学到了什么

    jquery后学什么 by Kara Luton 卡拉·卢顿(Kara Luton) 我在训练营两年后学到了什么 (What I've Learned Two Years Post-Bootcamp) ...

最新文章

  1. 漂亮的NavMenu导航控件
  2. ajax 访问mysql_AJAX 数据库实例
  3. 【无标题】物联网安全挑战:为什么企业现在必须评估它们
  4. 【Verilog HDL学习之路】第一章 Verilog HDL 数字设计总论
  5. ai驱动数据安全治理_人工智能驱动的Microsoft工具简介
  6. Jenkins环境搭建和部署项目的过程
  7. python列表切片和推导式思维导图_Python列表推导式使用
  8. 删除可视图中的类不能彻底避免它重新被编译
  9. dbentry mysql_DbEntry.Net(Lephone Framework) Access ORM:安装和简单使用
  10. NYOJ 表达式求最值305
  11. Java支付宝第三方登陆开发过程
  12. opencv-python——调用摄像头录制并保存视频
  13. 10g recyclebin与用户表空间限额
  14. 形态学 - 膨胀和腐蚀
  15. PHP关闭Notice错误提示
  16. 亚洲的音乐史料及其历史研究状况
  17. 电脑更换固态硬盘并安装系统
  18. 音频文件的结构与规范——RIFF和WAVE音频文件格式
  19. 百度云“资源”被和谐,两行代码帮你解决
  20. XGBoost的原理、公式推导

热门文章

  1. SourcesTree使用手册3:文件贮藏与删除
  2. 数据库、数据库系统、数据库管理系统
  3. bzoj 3382: [Usaco2004 Open]Cave Cows 3 洞穴里的牛之三(切比雪夫距离)
  4. 决策单调性Ⅰ:四边形不等式(bzoj 1563: [NOI2009]诗人小G)
  5. 静电场里非常有用的公式
  6. hashCode 一致性hash 算法
  7. python - super 寻找继承关系
  8. WeixinJSBridge API使用实例
  9. 2018/2/11 ELK技术栈之ElasticSearch学习笔记二
  10. 昆特牌Online——客户端用到的一些技术