Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)
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
3
1 2 3
1.000000000000000
4
1 2 3 4
2.000000000000000
10
1 10 2 9 3 8 4 7 5 6
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 三分搜索 (精度!)的更多相关文章
- codeforces 578c//Weakness and Poorness// Codeforces Round #320 (Div. 1)
题意:一个数组arr,一个数字x,要使arr-x的最大子段最小,问该最小值. 三分x,复杂度logn,内层是最大子段的模板,只能用n复杂度的.因为是绝对值最大,正负各求一次,取大的.精度卡得不得了,要 ...
- codeforces 578c - weekness and poorness - 三分
2017-08-27 17:24:07 writer:pprp 题意简述: • Codeforces 578C Weakness and poorness• 给定一个序列A• 一个区间的poornes ...
- 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 ...
- 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 ...
- Codeforces E. Weakness and Poorness(三分最大子列和)
题目描述: E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 578.C Weakness and Poorness
C. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【CodeForces】578 C. Weakness and Poorness
[题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...
- 三分搜索-ZOJ LightBulb
开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...
- hdu 4355 Party All the Time(三分搜索)
Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toget ...
随机推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
Let SS be a sequence of integers s_{1}s1, s_{2}s2, ......, s_{n}snEach integer is is associ ...
- 402 Remove K Digits 移掉K位数字
给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小.注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零.示例 1 :输入: ...
- MySQL的两种存储引擎storage engine特点和对比
MyISAM 优点:快速读取数据, 占用空间小 缺点:不支持事务,外键 (表级别锁) InnoDB 优点:支持事务,外键; 高性能(CPU效率高) 缺点: 慢,占空间 (行级别锁)
- 从 C++ 到 Objective-C 的快速指南
简介 当我开始为iOS写代码的时候,我意识到,作为一个C++开发者,我必须花费更多的时间来弄清楚Objective-C中怪异的东西.这就是一个帮助C++专家的快速指南,能够使他们快速的掌握Apple的 ...
- Spring.Net学习笔记(二)-数据访问器
Spring对ADO.NET也提供了支持,依赖与程序集Spring.Data.dll IDbProvider IDbProvider定义了数据访问提供器的基础,配置如下 <?xml versio ...
- 利用freemarker导出页面格式复杂的excel
刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...
- vscode使用教程(web开发)
1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...
- Linux学习笔记之Linux命令
1. blkid 查看当前系统中所有已挂载文件系统的类型
- Android 6.0权限分组
Android系统从6.0开始将权限分为一般权限和危险权限,一般权限指不涉及用户隐私的一些权限,比如Internet权限.危险权限指涉及获取用户隐私的一些操作所需要的权限,比如读取用户地理位置的权限. ...
- (转)中国电信友华PT921、PT921G光猫设置路由,无线WIFI设置
中国电信友华PT921.PT921G光猫设置路由,无线WIFI设置. 第一步,用管理员帐号密码登陆,打开浏览器,输入http://192.168.1.1 登陆帐号:telecomadmin登陆密码:n ...