Codeforces 578.C Weakness and Poorness
2 seconds
256 megabytes
standard input
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.
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 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.
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
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的增大减小而波动.不是二分,我也想不出好的数学式子来表示答案,只能三分法了.其实稍微分析一下发现真的满足单峰性.当x足够大时,所有的数都变成负数,x越大答案越大.当x足够小时所有的数都变成正数,x越小答案越小,所以这是一个向下凸的单峰函数.三分即可.
求最大的字段和的绝对值可以先求出一开始的序列的(答案为正的),然后将序列中的所有数取反,再来一次,两个答案取max.
注意精度问题.允许误差6位.(一开始没看到一直TLE......).
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n;
double a[],ans,b[],c[],eps = 3e-,sum1[],sum2[]; double C(double x)
{
double max1 = -100000.0,max2 = -100000.0;
sum1[] = sum2[] = 0.0;
for (int i = ; i <= n; i++)
{
b[i] = a[i] - x;
c[i] = -b[i];
}
for (int i = ; i <= n; i++)
{
sum1[i] = max(sum1[i - ] + b[i],b[i]);
max1 = max(max1,sum1[i]);
sum2[i] = max(sum2[i - ] + c[i],c[i]);
max2 = max(max2,sum2[i]);
}
return max(max1,max2);
} int main()
{
scanf("%d",&n);
for (int i = ; i <= n; i++)
scanf("%lf",&a[i]);
if (n == )
printf("%.15lf\n",);
else
{
double L = -20000.0,R = 20000.0;
while (R - L > eps)
{
double mid1 = L + (R - L) / 3.0,mid2 = R - (R - L) / 3.0;
if (C(mid1) < C(mid2))
{
ans = mid1;
R = mid2;
}
else
{
ans = mid2;
L = mid1;
}
}
printf("%.15lf\n",C(ans));
} return ;
}
Codeforces 578.C Weakness and Poorness的更多相关文章
- 【CodeForces】578 C. Weakness and Poorness
[题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...
- 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(Div.2)
codeforces #578(Div.2) A. Hotelier Amugae has a hotel consisting of 1010 rooms. The rooms are number ...
- [codeforces] 578C Weakness and Poorness || 三分
原题 题目定义了两个变量: poorness表示一个区间内和的绝对值. weakness表示一个所有区间最大的poornesss 题目要求你求一个x使得 a1 − x, a2 − x, ..., an ...
- 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 ...
- codeforces 578c//Weakness and Poorness// Codeforces Round #320 (Div. 1)
题意:一个数组arr,一个数字x,要使arr-x的最大子段最小,问该最小值. 三分x,复杂度logn,内层是最大子段的模板,只能用n复杂度的.因为是绝对值最大,正负各求一次,取大的.精度卡得不得了,要 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...
随机推荐
- 导出Office365中的组及成员
Set-ExecutionPolicy unrestricted $cred = Get-Credential $session = New-PSSession -ConfigurationName ...
- [leetcode-884-Uncommon Words from Two Sentences]
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- 扩展Lucas定理 扩展Lucas板子
题意概述:多组询问,给出N,K,M,要求回答C(N,K)%M,1<=N<=10^18,1<=K<=N,2<=M<=10^6 分析: 模数不为质数只能用扩展Lucas ...
- 全国城市一卡通一级TSM平台业务架构及意义
[导读]TSM平台是一种具有鲜明行业属性的平台,因此,各行业都建立了本行业的TSM平台.为促进城市一卡通行业移动支付的快速发展,住房和城乡建设部也建立了全国城市一卡通行业一级TSM平台. 作为住建部标 ...
- 20135208 JAVA第三次实验
课程:Java实验 班级:201352 姓名:贺邦 学号:20135208 成绩: 指导教师:娄佳鹏 实验日期:15.06.03 实验密级: ...
- MySort试验记录
MySort试验记录 编写目标 结果代码 思路 将数组每一个项目的第三组数字抽出来并且排序成12345的顺序,并形成新数组k3. 把原数组的每一项与k3进行比较,每符合一项便输出一项,从而重新排序出新 ...
- MDL数据结构
微软的文档里对MDL的描述感觉语焉不详,这两天在找工作的间隙逆向+黑盒测试了一下MmBuildMdlForNonPagedPool,把得到的一些理解描述下来. 一.MDL数据结构 MDL是用来建立一块 ...
- 补发9.26“天天向上”团队Scrum站立会议
组长:王森 组员:张金生 张政 栾骄阳 时间:2016.09.26 地点:612寝 组员 已完成 未完成 王森 可行性分析 找出设计亮点 张金生 寻找UI素材 设计用户操作 张政 搭建环境 基础逻辑框 ...
- 关于java读取excle文件的相关方法 ;
1.读取Excle文件内容的方法 拿过来可以直接用 : 2.参照 http://www.anyrt.com/blog/list/importexcel.html#6 更多知识请参考:http://ww ...
- php自带的filter过滤函数
PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. filter_has_var()检查是否存在指定输入类型的变量. filter_id()返回指定过滤器的 ID 号. filt ...