codeforces 939E Maximize! 双指针(two pointers)
3 seconds
256 megabytes
standard input
standard output
You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:
- Add a positive integer to S, the newly added integer is not less than any number in it.
- Find a subset s of the set S such that the value
is maximum possible. Here max(s) means maximum value of elements in s,
— the average value of numbers in s. Output this maximum possible value of
.
The first line contains a single integer Q (1 ≤ Q ≤ 5·105) — the number of queries.
Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 ≤ x ≤ 109) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.
It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes.
Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.
Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if
.
6
1 3
2
1 4
2
1 8
2
0.0000000000
0.5000000000
3.0000000000
4
1 1
1 4
1 5
2
2.0000000000 大意:一个多重集合(multiset,可以有重复元素的集合)刚开始为空,有两种操作:
1.加入一个数(保证加入的数大于等于集合中现有的数)
2.从multiset中选出一个子集,使得max-average最大,并输出。 YY:
1.由于插入元素的特殊性,这个多重集合用数组维护即可保证有序。
2.要使max-average最大,选出的子集大概是一个大哥带着一群小弟,而且这些小弟是从最小的开始且连续的。(一个较大的数和最小的几个数)
大概就是这样的————一个很大的数作为max,再选几个最小的数来拉低average。
3.如果选定一个max,以小弟的数量为自变量,max-average为因变量,这个函数是单峰的,使函数值最大的小弟数量称为“最佳小弟数量”。
4.对于选定的两个max,如果max1<max2,则 max2带着max1的小弟 一定比 max1带着自己的小弟 产生的答案(max-average)优,
而且如果max2带着比max1数量少的小弟 一定没有 max2带着max1的小弟 优。
所以选定最大值max2 取到最大max-average时,小弟数量一定大于等于 max1。 到这里,单调性非常明显,可以利用简洁的双指针法,一个变量pre代表小弟数量,maxx代表大哥是谁,sum代表当前累加和。
每加入一个数,算出以这个数为最大值时的最优解:
新的最大值带的小弟大于等于前一个最大值带的小弟,让新的最大值尝试带更多小弟,直到取到最大值(碰到了单峰函数的最大值)停止。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
}
int Q;
int a[],cnt,maxx,pre,temp;
long long sum;
double ans;
int main(){
//freopen("in","r",stdin);
Q=read();
while(Q--){
if(read()==){
temp=read();
a[++cnt]=temp;
sum+=temp-maxx;
maxx=temp;
while(pre<cnt&&maxx-1.0*sum/(pre+)<maxx-1.0*(sum+a[pre+])/(pre+))
sum+=a[++pre];
if(maxx-1.0*sum/(pre+)>ans)
ans=maxx-1.0*sum/(pre+);
}
else
printf("%.10f\n",ans);
}
return ;
}
ps:本题还可以利用单峰函数的性质,选定最大值,三分小弟数量。
codeforces 939E Maximize! 双指针(two pointers)的更多相关文章
- Codeforces 939E - Maximize!
939E - Maximize! 思路: 贪心:最后的集合是最大值+前k小个 因为平均值时关于k的凹形函数,所以可以用三分求最小值 又因为后面的k肯定比前面的k大,所以又可以双指针 三分: #incl ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- 2018.12.08 codeforces 939E. Maximize!(二分答案)
传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...
- Codeforces 939E Maximize ( 三分 || 二分 )
题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 : 首先关注 ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
- CodeForces 939E Maximize
Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...
- codeforces#1139E. Maximize Mex(逆处理,二分匹配)
题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...
- Codeforces 1139E Maximize Mex 二分图匹配
Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...
- Pudding Monsters CodeForces - 526F (分治, 双指针)
大意: n*n棋盘, n个点有怪兽, 求有多少边长为k的正方形内恰好有k只怪兽, 输出k=1,...,n时的答案和. 等价于给定n排列, 对于任意一个长为$k$的区间, 若最大值最小值的差恰好为k, ...
随机推荐
- CAD在一个点构造选择集
主要用到函数说明: IMxDrawSelectionSet::SelectAtPoint 在一个点构造选择集.详细说明如下: 参数 说明 [in] IMxDrawPoint* point 点坐标 [i ...
- JS中的let和var的区别
最近很多前端的朋友去面试被问到let和var的区别,其实阮一峰老师的ES6中已经很详细介绍了let的用法和var的区别.我简单总结一下,以便各位以后面试中使用. ES6 新增了let命令,用来声明局部 ...
- [HNOI]2003 消防局的建立
消防局的建立 本题地址:http://www.luogu.org/problem/show?pid=2279 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料 ...
- cookie的原理
一般来说,Cookie通过HTTP Headers从服务器端返回到浏览器上.首先,服务器端在响应中利用Set-Cookie header来创建一个Cookie ,然后,浏览器在它的请求中通过Cooki ...
- TestNG多线程测试-注解方式实现
用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...
- JAVA之文件操作1,2,3,4
package first_program; import java.io.File; import java.io.IOException; public class num_1v { public ...
- CodeForcesGym 100753F Divisions
Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Or ...
- Frame Stacking 拓扑排序 图论
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ .... ...
- 夜话JAVA设计模式之单例模式(单件模式Singleton)
单例模式也叫单件模式,就是确保一个类只有一个实例,并提供一个全局访问点. 设计成单例即把某个类设计成我们自己管理的单独实例,避免实例对象的重复创建,我们只有通过单例类的全局访问点获取实例. 下面来看金 ...
- 非常适合新手的jq/zepto源码分析05
zepto的原型 $.fn 属性: constructor //构造行数 forEach: emptyArray.forEach, //都是原生数组的函数reduce: emptyArray.re ...
