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

HINT

题意

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

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

题解:

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

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

感觉尺取法也行

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define maxn 2000000 + 500
  22. #define mod 10007
  23. #define eps 1e-9
  24. int Num;
  25. char CH[];
  26. //const int inf=0x7fffffff; //нчоч╢С
  27. const int inf=0x3f3f3f3f;
  28. /*
  29.  
  30. inline void P(int x)
  31. {
  32. Num=0;if(!x){putchar('0');puts("");return;}
  33. while(x>0)CH[++Num]=x%10,x/=10;
  34. while(Num)putchar(CH[Num--]+48);
  35. puts("");
  36. }
  37. */
  38. //**************************************************************************************
  39. double a[];
  40. double g[];
  41. int n;
  42. double r,l,mid1,mid2;
  43. double check(double mid)
  44. {
  45. double q1=,q2=,q3=;
  46. for(int i=;i<=n;i++)
  47. g[i]=a[i]-mid;
  48. for(int i=;i<=n;i++)
  49. q1=max(g[i],g[i]+q1),q3=max(q3,q1);
  50. for(int i=;i<=n;i++)
  51. g[i]=g[i]*(-1.0);
  52. for(int i=;i<=n;i++)
  53. q2=max(g[i],g[i]+q2),q3=max(q3,q2);
  54. return q3;
  55. }
  56.  
  57. int main()
  58. {
  59. cin>>n;
  60. for(int i=;i<=n;i++)
  61. scanf("%lf",&a[i]);
  62. r = 30000.0;l = -30000.0;
  63. while(r-l>1e-)
  64. {
  65. double mid1=(l+l+r)/3.0;
  66. double mid2=(l+r+r)/3.0;
  67. if(check(mid1)>=check(mid2))
  68. l = mid1;
  69. else
  70. r = mid2;
  71. }
  72. printf("%.13lf\n",check((l+r)/2.0));
  73. }

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. Hive简介

    实验简介 我们本节课程主要介绍 Hive 的相关知识,将会涉及以下内容: Hive 的定义 Hive 的体系结构 Hive 与关系数据库的区别 Hive 的应用场景 Hive 的存储 一.什么是 Hi ...

  2. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  3. apache开源项目--Sirona

    Apache Sirona 为 Java 应用程序提供了一个简单但可扩展的监控解决方案. apache / sirona

  4. ORACLE DATAGURARD配置手记

    经过多次实践,参阅网上N多文章……最后还是配不成,可能本人悟性太低,无法体会高手的笔记.最终还是在前辈的帮助下完成.特用最平实的手法记录下来,以便如吾辈菜鸟能 看得懂. 运行Data Guard的条件 ...

  5. 多线程程序设计学习(6)Producer-Consumer模式

    Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...

  6. 【转】【iOS】导航栏那些事儿

    原文网址:http://www.jianshu.com/p/f797793d683f 参考文章 navigationItem UINavigationItem UINavigationBar UIBa ...

  7. 【转】Git详解之一:Git起步

    原文网址:http://blog.jobbole.com/25775/ 原文:<Pro Git> 起步 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后 ...

  8. suds 在python3.x上的安装并访问webservice

    类库安装 直接使用命令行:pip install suds 报错:Traceback (most recent call last):    File "setup.py", li ...

  9. 网页推送库 SignalR

    SignalR 可以用这个库开发网页聊天室应用 ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程.实时 Web 功能 ...

  10. html中a标签中的onclick和href的使用

    1. 链接的 onclick 事件被先执行,其次是 href 属性下的动作(页面跳转,或 javascript 伪链接): 假设链接中同时存在 href 与 onclick,如果想让 href 属性下 ...