C. Weakness and Poorness

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/578/problem/C

Description

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 Input

3
1 2 3

Sample Output

1.000000000000000

HINT

题意

给你n个数之后,让这n个数都减去一个x后

使得这n个数中的某个线段,的连续和的绝对值的最大值最小

题解:

这个是一个凸性的函数,符合三分

于是就三分咯,check是用一个dp来check,是on的

感觉尺取法也行

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000000 + 500
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
double a[];
double g[];
int n;
double r,l,mid1,mid2;
double check(double mid)
{
double q1=,q2=,q3=;
for(int i=;i<=n;i++)
g[i]=a[i]-mid;
for(int i=;i<=n;i++)
q1=max(g[i],g[i]+q1),q3=max(q3,q1);
for(int i=;i<=n;i++)
g[i]=g[i]*(-1.0);
for(int i=;i<=n;i++)
q2=max(g[i],g[i]+q2),q3=max(q3,q2);
return q3;
} int main()
{
cin>>n;
for(int i=;i<=n;i++)
scanf("%lf",&a[i]);
r = 30000.0;l = -30000.0;
while(r-l>1e-)
{
double mid1=(l+l+r)/3.0;
double mid2=(l+r+r)/3.0;
if(check(mid1)>=check(mid2))
l = mid1;
else
r = mid2;
}
printf("%.13lf\n",check((l+r)/2.0));
}

Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp的更多相关文章

  1. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)

    显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...

  2. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...

  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game

    题目链接:http://codeforces.com/contest/578/problem/B 题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作. ...

  4. 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 ...

  5. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】

    A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)

    D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值

    E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B "Or" Game (贪心)

    首先应该保证二进制最高位尽量高,而位数最高的数乘x以后位数任然是最高的,所以一定一个数是连续k次乘x. 当出现多个最高位的相同的数就枚举一下,先预处理一下前缀后缀即可. #include<bit ...

  9. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...

随机推荐

  1. 大四实习准备5_android广播机制

    2015-5-1 android 广播机制 5.1简介 分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先 ...

  2. poj3254

    还是那句老话:dp关键在状态: 求有多少种排布方式,是任意两头牛不相邻(有些地方不能放): 不用心,一开始还纠结了半天 和之前USACO上某题方法是一样的,每一行放或不放只有两种情况 把它当作一个二进 ...

  3. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

  4. 每天一个linux 命令:which

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  5. O(1)时间删除链表的已知结点

    这题并不需要从头结点遍历到已知结点,只需要知道已知结点,将改结点下一个结点赋值给它,再删除这个下一个结点就行,其中还需要考虑各种情况. 1)链表为空或者已知结点为空 2)链表只有一个结点,这个结点就是 ...

  6. linux扩展权限

    扩展权限包括s,g,t 对于创建文件或文件夹由umask值来决定共默认权限 普通用户默认是0002 root有户是0022 目录的默认权限是777-umask(普通用户775 root是755) 文件 ...

  7. 一键生成HTML4和WAP站

    前两天在QQ空间上提到微信上线风铃时,把其中的HTML5错打成HTML4,结果发现很多媒体微博照着转载,依旧说成 "一键生成HTML4和WAP站",这就是转载不注明出处.不署名.不 ...

  8. bzoj 1411 [ZJOI2009]硬币游戏

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1411 [题意] N个硬币放在一个有2*N个位置的圆桌上,求T次操作后的情况.对于一个操 ...

  9. bzoj 1058 [ZJOI2007]报表统计(set)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1058 [题意] 一个序列,提供插入,查询相邻最小差值,查询任意最小差值的操作. [思路 ...

  10. 在PHPmyadmin中删除数据库

    删除数据库用sql语句 的方法:   删除数据库DROP DATABASE `数据库名称`; 删除数据库里的表DROP TABLE `数据库里的表名`;