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 ...
随机推荐
- 高可用Kubernetes集群-4. kubectl客户端工具
六.部署kubectl客户端工具 1. 下载 [root@kubenode1 ~]# cd /usr/local/src/ [root@kubenode1 src]# wget https://sto ...
- linux 下 mysql安装和配置
最近在学习R语言,看到R与数据库交互这一部分,就自己动手实践了一下,数据库选择的是mysql,主要记录下linux下怎么安装mysql. 网上的很多资料都有相关的文章,这里只是记录下自己安装过程中遇到 ...
- VLP16线用户手册.md
VLP16线用户手册 文档 传感器数据 分组类型和定义 传感器产生两种类型的数据包:数据包和位置数据包.位置包有时也被称为遥测包或GPS包. 数据包包括传感器测量到的三维数据以及返回光脉冲的表面的校 ...
- 图论---POJ 3660 floyd 算法(模板题)
是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...
- HDU 5869 Different GCD Subarray Query rmq+离线+数状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...
- 读我是一只it小小鸟有感!!!
<<我是一只it小小鸟>>是老师为我们这些即将步入it行业的新人推荐的一本书,通过这本书的简介知道它是由一群it学子共同创造而成的,每个人分别讲述各自的成长经历.书的开篇是本书 ...
- NULL指针引起的一个linux内核漏洞
NULL指针一般都是应用于有效性检测的,其实这里面有一个约定俗成的规则,就是说无效指针并不一定是 NULL,只是为了简单起见,规则约定只要指针无效了就将之设置为NULL,结果就是NULL这个指针被用来 ...
- phpcms 发布时间 更新 时间
- 用Python实现求Fibonacci数列的第n项
1. 背景——Fabonacci数列的介绍(摘自百度百科): 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacc ...
- jquery validate 一个注册表单的应用
先看页面 前端表单代码 register.html <form class="mui-input-group" id="regForm"> < ...