CF578C Weakness and Poorness
题面:给一个序列中的,每一个数都减去一个实数x,使得到的新序列的max(最大连续和,|最小连续和|)最小。(|ai| <= 10000)
感性的想想,会发现最大连续和随x变大而变小,最小连续和随x变大而变大。
严格的证明:首先对于任意区间[L, R],|∑ai - x|一定是一个绝对值函数,则最大连续和就是把所有[L, R]的函数复合在一起,然后取max,画图可知,也是一个单峰函数。(似乎也不怎么严格)
那么max(最大连续和,|最小连续和|)就是一个单峰函数,于是用三分求解。
至于最大连续和的求法,O(n)扫一遍即可,看代码就懂了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 2e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, a[maxn]; db calc(db x)
{
db Min = , Max = , ans = ;
for(int i = ; i <= n; ++i)
{
Min = Min + a[i] - x > ? : Min + a[i] - x;
Max = Max + a[i] - x < ? : Max + a[i] - x;
ans = max(ans, max(Max, -Min));
}
return ans;
} int main()
{
n = read();
for(int i = ; i <= n; ++i) a[i] = read();
db L = -, R = , x;
for(int i = ; i <= ; ++i)
{
db m1 = L + (R - L) / 3.00;
db m2 = R - (R - L) / 3.00;
if(calc(m1) <= calc(m2)) x = m1, R = m2;
else x = m2, L = m1;
}
printf("%.8lf\n", calc(x));
return ;
}
CF578C Weakness and Poorness的更多相关文章
- cf578c Weakness and Poorness 三分
其实三分就是一个求单峰函数的最值的东西,用法比较统一.这个题就是观察发现不美好值是一个单峰函数,然后枚举t进行三分就行了. 题干: 给定一个长度为n的数组ai,求一个实数x,使得序列a1-x,a2-x ...
- 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 578.C Weakness and Poorness
C. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【CodeForces】578 C. Weakness and Poorness
[题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...
- Codeforces E. Weakness and Poorness(三分最大子列和)
题目描述: E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- CF578C:Weakness and Poorness——题解
https://vjudge.net/problem/CodeForces-578C —————————————————————————— 题目大意:序列的数-x,求最大连续子序列和的绝对值的最小值. ...
- [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 ...
随机推荐
- linux 安装maven,注意下载-bin.tar.gz文件
先去http://maven.apache.org/download.cgi下载对应的版本然后放到服务器上/var/local文件夹下面, 此处使用的是apache-maven-3.5.2-bin.t ...
- ansile 命令解释选项
1, -a MODULE_ARGS --args=MODULE_ARGS 作用传递参数给模块使用 2, --ask-vault-pass 执行时询问vault的密码 3, -B SECONDS --b ...
- Java基础21-构造函数之间的调用
public class Test{ public static void main(String[] args){ Persion p2=new Persion("小明",22) ...
- SEO艺术阅读笔记
SEO(Search Engine Optimization)搜索引擎优化 搜索引擎:反映认知,连接贸易搜索引擎基础百度搜索高级语法确定SEO目标,定义网站受众设定SEO目标开发前定制SEO方案理 ...
- Linux Nginx环境安装配置redmine3.1
作者博文地址:https://www.cnblogs.com/liu-shuai/ 环境: CentOS-6.5+Nginx-1.8.0+Redmine-3.1.1+Ruby-2.0 1.配置环境 1 ...
- (转)2017年最新企业面试题之shell(一,二)
2017年最新企业面试题之shell(一) ********************************************** 企业Shell面试题1:批量生成随机字符文件名案例 * *** ...
- 关于kernel的疑问,不解
(1)最近看的关于linux的部分主要是底层部分,linux对设备是如何访问,结果还是通过地址.机制差不错知道,下一步是阅读相关代码进一步理清内核,同时了解驱动的开发. 主要的疑问是对于高端内存映射, ...
- zookeeper 编程框架 curator
Curator框架提供了一套高级的API, 简化了ZooKeeper的操作. 它增加了很多使用ZooKeeper开发的特性,可以处理ZooKeeper集群复杂的连接管理和重试机制. 这些特性包括: 自 ...
- linux_api之进程环境(二)
本篇索引: 1.引言 2.终端登录 3.进程组 4.会话期 1.引言 通过上一篇的学习,我们已经知道了如何控制一个进程,fork函数从父进程中复制出子进程,我们可以通过exec函数让子进程运行新的 ...
- CSS+DIV进度条
<style type="text/css"> .Bar { position: relative; width: 200px; /* 宽度 */ border: 1p ...