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 ...
随机推荐
- MySQL数据库插入中文时出现Incorrect string value: '\xE6\x97\xB7\xE5\x85\xA8' for column 'sz_name' at row 1
今天在开发时候出现了这个问题 Incorrect string value: '\xE6\x97\xB7\xE5\x85\xA8' for column 'sz_name' at row 1 场景,往 ...
- (转)linux sed命令就是这么简单
sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...
- 第十八章:自定义splash screen、app icon和tab icon
本文介绍如何自定义splash screen.app icon和tab icon. 1. 自定义splash screen和app icon 1.1 产生resources文件夹,启动命令行: ion ...
- maven 基本配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- bootstrap --datetimepicker之时间段选择
文件引入 <script type="text/javascript" src="css/jquery-3.2.1.js"></script& ...
- 分支结构case……end
语法: case when 条件1 then 结果1 when 条件2 then 结果2 ……….. else 其它结果 end 执行顺序: 条件1成立执行结果1 条件2成立执行结果2 如果所有的wh ...
- C#语言-02.数据类型
a. 数据类型 i. 值类型:是一种由类型的实际值表示的数据类型,存储在栈内的存储空间中,由于编译器编译后将源代码中的值类型变量直接对应到唯一的存储空间上,直接访问该存储空间,故值类型的数据具有较快地 ...
- 《Java开发实战经典》读书笔记
Java常用的内存区域: (1) 栈内存空间:保存所有的对象名称. (2) 堆内存空间:保存每个对象的具体属性内容. (3) 全局数据区:保存static类型的属性. ( ...
- (二)HTML中的部分标签
HTML作为一种超文本标记语言,其中用到了大量的标签,昨天主要看了HTML中的图像标签和表格标签. (一)图像标签 <img> <img src="url"/&g ...
- C# 获得word中某一段落所在页的页码
方式一:通过openxml 从xml结构里获得不可行.原因如下A footer is not on a page and a page number in a footer is a field th ...