NOIP模拟 最佳序列 - 二分 + 单调队列
题意:
各一个n(\(\le 20000\))的序列,定义纯洁序列为长度len满足\(L \le len \le R\)的序列,纯洁值为某一纯洁序列的平局值,输出所有纯洁序列中最大平均值。
分析:
二分 + 单调队列:二分出平均值mid, 下面来判断该平均值是否符合加大L的要求:
\]
当mid偏小时:\(\sum{(a_i - mid)} >= \sum{(a_j - mid)} (i - r + 1 <= j <= i - l + 1)\)
若\(min\{ \sum(a_j - mid) (i - r + 1 <= j <= i - l + 1)\} <= \sum{a_i - mid}\)那么就肯定满足了。
即要求指定区间中的最小值,直接单调队列。\(O(nlogn)\)
code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<deque>
using namespace std;
namespace IO {
template<typename T>
inline void read(T &x) {
T i = 0, f = 1;
char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 2e4 + 5;
#define eps 1e-8
int n, l, r, a[N];
typedef long long ll;
double sum[N], ans;
deque<double> que;
inline bool calc(double mid){
sum[0] = 0;
for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + 1.0 * a[i] - mid;
que.clear();
for(int i = l; i <= n; i++){
while(!que.empty() && sum[(int)que.back()] >= sum[i - l]) que.pop_back();
que.push_back(i - l);
while(!que.empty() && i - que.front() > r) que.pop_front();
if(!que.empty() && sum[i] >= sum[(int)que.front()]) return true;
}
return false;
}
int main(){
freopen("h.in", "r", stdin);
read(n), read(l), read(r);
for(int i = 1; i <= n; i++) read(a[i]);
double L = 0, R = 1e6;
for(int t = 1; t <= 50; t++){
double mid = (L + R) / 2;
if(calc(mid)) L = mid;
else R = mid;
}
printf("%.4f", L);
return 0;
}
NOIP模拟 最佳序列 - 二分 + 单调队列的更多相关文章
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
- [USACO12MAR]花盆 二分 单调队列
[USACO12MAR]花盆 二分 单调队列 存在一个长度为\(x\)的区间\([l,r]\),使得区间中最大值与最小值差至少为\(w\),求这个最小的\(x\) \(n\le 100000\),\( ...
- hdu_5884_Sort(二分+单调队列)
题目链接:hdu_5884_Sort 题意: 有n个数,每个数有个值,现在你可以选择每次K个数合并,合并的消耗为这K个数的权值和,问在合并为只有1个数的时候,总消耗不超过T的情况下,最小的K是多少 题 ...
- hdu-3276-dp+二分+单调队列
Star Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 2018.09.26洛谷P3957 跳房子(二分+单调队列优化dp)
传送门 表示去年考普及组的时候失了智,现在看来并不是很难啊. 直接二分答案然后单调队列优化dp检验就行了. 注意入队和出队的条件. 代码: #include<bits/stdc++.h> ...
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- 【BZOJ2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化DP
[BZOJ2806][Ctsc2012]Cheat Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的 ...
- Gym 100801J Journey to the "The World's Start"(二分+单调队列)
题意: 现在有1,2,3...N这N个站, 给定限定时间Limt, N-1种票的价格, 分别对应一个最远距离, 叫你选择一种票, 满足可以在规定时间到达N站台,而且价格最低 思路: 如果买距离为L ...
随机推荐
- transform属性实现翻转效果
transform:perspective(800px) rotateY(180deg);//翻转180度,透视800px; transition-delay: 0.3s;//过程时间 opacity ...
- SD卡与tf卡引脚转换
https://www.cnblogs.com/shangdawei/p/3255414.html
- json问题小结
json 键值对增加.删除 obj.key='value'; // obj.key=obj[key]=eval("obj."+key); delete obj.key; vue中新 ...
- [RxJS] Avoid mulit post requests by using shareReplay()
With the shareReplay operator in place, we would no longer fall into the situation where we have acc ...
- 在Windows下搭建Apacheserver
Apacheserver是一款基于HTTP协议的webserver.Apacheserver使用CGI开发 首先下载Apacheserver,下载地址http://download.csdn.net/ ...
- LeetCode Algorithm 01_Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- angular 创建服务
一:新建服务模块和服务文件 ng g module services --spec=false ng g service services/quote --spec=false 二:在quote.se ...
- Geodatabase模型
原文 Geodatabase模型 地理数据模型是地理实体及其关系的形式化抽象和数学描述.随着数据库.面向对象等技术的发展,面向对象的地理数据模型成为大型空间数据库的首选方案,它克服了传统地理数据模型的 ...
- ios sqlite数据库操作
@interface MyViewController () { // 数据库实例,代表着整个数据库 sqlite3 *_db; } @end @implementation MyViewContro ...
- 算法 Tricks(六)— if 条件分支的简化
考虑下面的三分支的定义式: f=⎧⎩⎨⎪⎪a,b,a+b,x>yx<yx=y int f = 0; if (x >= y) f += a; if (x <= y) f += b ...