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 ...
随机推荐
- 【snmp】Linux开启snmp及查询
1.Linux snmp 1.安装snmp yum install -y net-snmp* 2.备份snmp配置 cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.co ...
- C#判断字符串中是否有数字
// <summary> /// 提取字符串中的数字字符串 /// </summary> /// <param name="str"></ ...
- [Ubuntu] sogou中文输入法安装
I install sogou 中文输入法 successfully, after following below steps: 1. install sogou pingyin by deb pac ...
- JAVA里面json和java对象之间的相互转换
1. 把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromObject(list); String jsonstr = ar ...
- lintcode-442-实现 Trie
442-实现 Trie 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 注意事项 你可以假设所有的输入都是小写字母a-z. 样例 insert(&qu ...
- C#高级编程 (第六版) 学习 第二章:C#基础
第二章 基础 1,helloworld示例: helloworld.cs using System; using System.Collections.Generic; using System.Li ...
- WPF/MVVM快速指引
简介 最近微软推出了UWA,又是一波新的C#+xaml学习热.好多小伙伴都对MVVM感觉很好奇,但是有些地方也有点难以理解.特意写了这边文章,希望对你有帮助. 这边文章会很长,所以我会用几个例子的形式 ...
- 使用Python 、 go 语言测试rabbitmq的工作机制
1:在haproxy 和 rabbitmq上安装Python.python2-pip,默认是Python2 yum install -y python python2-pip 2:在haproxy ...
- (一)MySQL基础篇
1.mysql简介 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库. 主流的数据库有:sqlserver,mysql,Oracle.SQLite.Access.MS SQL Se ...
- 敏捷冲刺DAY5
一. 每日会议 1. 照片 2. 昨日完成工作 发布和提供需求功能的实现 用户修改自己的信息 用户界面设计 管理员界面设计 3. 今日完成工作 4. 工作中遇到的困难 1.设置的背景无法显示. 2.一 ...