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. DeepCloneObjects 和 DeepClone

    ARX AcDbDatabase 中的方法 deepCloneObjects() 和 wblock() 区别以及和 AcDbObject 方法 clone() 和 deepClone() 的关系 Ac ...

  2. IOS 11,UIWebView内容随状态栏高度下移,导致状态栏不透明

    解决方案: 方法1:在html中设置 <meta name="viewport" content="viewport-fit=cover,maximum-scale ...

  3. vue基础---列表渲染

    首先简单回顾下v-for‘指令 <ol id="list_area"> <li v-for="book in books">{{book ...

  4. 小程序wx:key = “{{*this}}”报错

    解决方案:改为 wx:key = "*this"

  5. Number Theory Problem(The 2016 ACM-ICPC Asia China-Final Contest 找规律)

    题目: Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is in ...

  6. Linux下查看Tomcat运行日志

    查看方式 1.先切换到:cd usr/local/tomcat5/logs2.tail -f catalina.out3.Ctrl+c 是退出tail命令/alt+E+R重置 部署常用指令 1.ps ...

  7. 爬虫之Requests库

    官方文档:http://cn.python-requests.org/zh_CN/latest/ 一.引子 import requests resp = requests.get("http ...

  8. Balanced Numbers(数位dp)

    Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...

  9. ebay 如何获取用户token

    1. 首先 配置环境加载依赖的ebay SDK 下载地址 https://go.developer.ebay.com/ebay-sdks 需要在本地仓库安装下面的jar mvn install:ins ...

  10. HDU 1212 大整数的取模运算

    因为这里是MOD最大为100000 所以我将字符串看作5个一组,并记录后面跟了多少个100000 每次取5个数根据其数据进行取模更新 注意过程中 100000*100000会超int #include ...