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 ...
随机推荐
- 1051 复数乘法(C#)
一.题目内容如下: 复数可以写成 ( 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 1:也可以写成极坐标下的指数形式 (,其中 R 是复数模,P 是辐角,i 是虚数单位,其等价于三角 ...
- xposed源码编译与集成
xposed installer3.0版本之后,传统的xposed框架的使用方法是从官网上下载xposed installer.apk以及xposed-arm-sdk22.zip包.然后具体的使用方法 ...
- Asp.Net 开发实战技术
1.什么是WMI技术 WMI是一项核心的Windows管理技术,WMI作为一种规范和基础结构,通过它可以访问.配置.管理和监视几乎所有的Windows资源,比如用户可以在远程计算机器上启动一个进程:设 ...
- [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则 ...
- 贪心 HDOJ 5355 Cake
好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...
- 树形DP URAL 1039 Anniversary Party
题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...
- c/c++导出lua绑定
[转载]https://note.youdao.com/share/?id=0f4132271151c4b62f9afb712e8304d9&type=note#/ 1.在纯C环境下,把C函数 ...
- [转]How can I get my webapp's base URL in ASP.NET MVC
本文转自:http://stackoverflow.com/questions/1288046/how-can-i-get-my-webapps-base-url-in-asp-net-mvc May ...
- JSON基础 JS操作JSON总结
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- LR接口测试---socket
前提条件: 编译:javac TcpServer.java 启动:java TcpServer ============================================ 代码示例: # ...