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 ...
随机推荐
- Map接口常用实现类学习
HashMap 1.6的HashMap:数组加单向链表结构 最重要的内部类Entry,全类名是java.util.HashMap.Entry,是个静态类,实现了Map.Entry接口.HashMap. ...
- like模糊查询%注入问题
android like 全局模糊查找文件命名 通过条件通过 like %search% 如果查找的关键字是% 那么就成了 like %%% 就会查找出所有的文件 解决办法是先把正则里面的匹配符 替换 ...
- select 点击的时候获取 value值
<select onchange="selectUrl1(this.options[this.options.selectedIndex].value);"> ...
- java中创建User Libray
第一步:右键项目==>Build Path ==>Configure Build Path... 第二步:选择Libraries==>点击 Add Library.. 第三步:选择U ...
- URAL 1252 ——Sorting the Tombstones——————【gcd的应用】
Sorting the Tombstones Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- ubuntu命令行添加拥有管理员权限新用户
最近买了个服务器,只有一个root用户,天天登录挺不方便的,所以想要新建用户;之前在本地都是用界面话新建的用户,这次记录一下学习命令行新建用户的过程: 第一步 : # sudo adduser zhq ...
- 深入理解JavaScript系列(30):设计模式之外观模式
介绍 外观模式(Facade)为子系统中的一组接口提供了一个一致的界面,此模块定义了一个高层接口,这个接口值得这一子系统更加容易使用. 正文 外观模式不仅简化类中的接口,而且对接口与调用者也进行了解耦 ...
- H5新特性-----type=file文件上传
1.语法 单文件上传:<input type="file" id="file1"/> 多文件上传:<input type="file ...
- 【数据库】7.0 MySQL入门学习(七)——MySQL基本指令:帮助、清除输入、查询等
1.0 help == ? 帮助指令,查询某个指令的解释.用法.说明等.详情参考博文: [数据库]6.0 MySQL入门学习(六)——MySQL启动与停止.官方手册.文档查询 https://www. ...