E. Maximize!
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:

  1. Add a positive integer to S, the newly added integer is not less than any number in it.
  2. 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 .
Input

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

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 .

Examples
input

Copy
6
1 3
2
1 4
2
1 8
2
output
0.0000000000
0.5000000000
3.0000000000
input

Copy
4
1 1
1 4
1 5
2
output
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)的更多相关文章

  1. Codeforces 939E - Maximize!

    939E - Maximize! 思路: 贪心:最后的集合是最大值+前k小个 因为平均值时关于k的凹形函数,所以可以用三分求最小值 又因为后面的k肯定比前面的k大,所以又可以双指针 三分: #incl ...

  2. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  3. 2018.12.08 codeforces 939E. Maximize!(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...

  4. Codeforces 939E Maximize ( 三分 || 二分 )

    题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 :  首先关注 ...

  5. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  6. CodeForces 939E Maximize

    Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...

  7. codeforces#1139E. Maximize Mex(逆处理,二分匹配)

    题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...

  8. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  9. Pudding Monsters CodeForces - 526F (分治, 双指针)

    大意: n*n棋盘, n个点有怪兽, 求有多少边长为k的正方形内恰好有k只怪兽, 输出k=1,...,n时的答案和. 等价于给定n排列, 对于任意一个长为$k$的区间, 若最大值最小值的差恰好为k, ...

随机推荐

  1. python vars模块

    {'__file__': 'C:/Users/zhou/PycharmProjects/fullstack2/6_20/test.py', '__doc__': None, '__cached__': ...

  2. Luogu P4549 裴蜀定理 / Min

    思路 题目已经给出了正解.我们只需要将裴蜀定理推广到若干数的线性组合就可以做这道题了 要注意的是需要在输入的时候取一个绝对值.因为可能会有负数存在.我之前也写过裴蜀定理的证明,要看的话点这里 吐槽 第 ...

  3. JAVA实现创建Excel表并导出(转发)

    <span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...

  4. JDBC在Java Web中的应用

    JDBC在Java Web中的应用 制作人:全心全意 在Java Web开发中,JDBC的应用十分广泛.通常情况下,Web程序操作数据库都是通过JDBC实现,即使目前数据库方面的开源框架层出不穷,但其 ...

  5. 【Codeforces 501C】Misha and Forest

    [链接] 我是链接,点我呀:) [题意] 给你一棵树 但是每个节点只告诉你出度个数 以及所有和它相连的点的异或和. 让你还原这棵树 [题解] 叶子节点的话,他所有节点的异或和就是它那唯一的一个爸爸 因 ...

  6. HDU 482 String

    String Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 482 ...

  7. [luoguP1879] [USACO06NOV]玉米田Corn Fields(DP)

    传送门 说要统计方案,感觉就是个 Σ 而矩阵中只有 01 ,可以用二进制表示 这样,预处理出每一个每一行所有可能的状态 s 然后初始化第一行所有状态的方案数为 1 f[i][j] = Σf[i - 1 ...

  8. reids桌面管理工具:RedisDesktopManager下载、使用

    概要:一款好用的Redis桌面管理工具,支持命令控制台操作,以及常用,查询key,rename,delete等操作. 下载软件,请点击下面链接,进入下载页,选择对应版本: https://redisd ...

  9. Yet another Number Sequence 矩阵快速幂

    Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...

  10. HDU——3579 Hello Kiki

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...