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
题目大意:有一个集合,两种操作:1.往集合里添加一个数,这个数比集合里的所有数都小. 2.求集合的一个子集,使得子集中最大值-平均值尽可能大.
分析:题目说每次插入的数比集合中的数都要小让我想到了二分.
   首先集合中的最大的数x是肯定要选的,因为如果最终选了n个数,那么这个数的贡献就是(n - 1) * x / n.那么现在的任务就是使得平均值尽可能小.很容易想到取尽量多的小数,让平均值最小.但是有数量限制,每次都挑尽量多的小数不一定平均值最小.二分选多少个数?这看不出什么单调性啊......在纸上推一推几个例子,可以发现这其实是一个单峰函数,三分法就好了.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
typedef long long ll;
const double inf = 1e20; ll q,tot,a[],sum[],n; double check(ll x)
{
if (x == )
return a[n];
double temp = sum[x] + a[n];
return temp / (double)(x + );
} int main()
{
scanf("%I64d",&q);
while (q--)
{
ll id;
scanf("%I64d",&id);
if (id == )
{
ll x;
scanf("%I64d",&x);
a[++n] = x;
sum[n] = sum[n - ] + a[n];
}
else
{ ll l = ,r = n,cnt = ;
while (l < r && cnt <= )
{
cnt++;
ll m1 = l + (r - l) / ,m2 = r - (r - l) / ;
if (check(m1) > check(m2))
l = m1;
else
r = m2;
}
double ans = inf;
for (ll i = l; i <= r; i++)
ans = min(ans,check(i));
printf("%.10lf\n",a[n] - ans);
}
} return ;
}
 

Codeforces 939.E Maximize!的更多相关文章

  1. CodeForces 939E Maximize

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

  2. Codeforces 939 D Love Rescue

    Love Rescue 题意:Valya 和 Tolya 是一对情侣, 他们的T恤和头巾上都有小写字母,但是女朋友嫌弃男朋友上T恤上的字不和她的头巾上的字一样,就很生气, 然后来了一个魔法师, 它可以 ...

  3. Codeforces 939 时区模拟 三分

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  4. Codeforces Round #464 (Div. 2) E. Maximize!

    题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...

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

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

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

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

  7. Codeforces 939E - Maximize!

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

  8. codeforces 939E Maximize! 双指针(two pointers)

    E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

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

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

随机推荐

  1. 【xml_Class、xmlElementNode_Class 类】使用说明

    xml_Class.xmlElementNode_Class这两个类是针对XML相关操作的类. 1.xml_Class类是针对XML文档操作的类 目录: 类型 原型 参数 返回 说明 Sub Sub ...

  2. 高可用Kubernetes集群-4. kubectl客户端工具

    六.部署kubectl客户端工具 1. 下载 [root@kubenode1 ~]# cd /usr/local/src/ [root@kubenode1 src]# wget https://sto ...

  3. Jenkins之Sonar 代码检查

    一.简介 SonarQube 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等 ...

  4. Beta阶段第二次网络会议

    Beta阶段第二次网络会议 第一次会议问题解决情况 画面问题已经解决,游戏提示信息已加入完成 不同情况下背景已加入完成,但细节部分仍需要进行调整 科技树添加完成,权限修改完成,还存在部分细节调整 AI ...

  5. 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列

    package com.wangcf; /** * 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列 * @author fan ...

  6. 四则运算3+psp0

    题目要求: 1.程序可以判断用户的输入答案是否正确,如果错误,给出正确答案,如果正确,给出提示. 2.程序可以处理四种运算的混合算式. 3.要求两人合作分析,合作编程,单独撰写博客. 团队成员:张绍佳 ...

  7. 浅谈Java变量的初始化顺序详解

    规则1(无继承情况下):对于静态变量.静态初始化块.变量.初始化块.构造器,它们的初始化顺序依次是(静态变量.静态初始化块)>(变量.初始化块)>构造器证明代码: 复制代码 代码如下: p ...

  8. Java 常用类String类、StringBuffer类

    常用类 String类.StringBuffer类 String代表不可变的字符序列 "xxxxxxx"为该类的对象 举例(1) public class Test { publi ...

  9. 前端系列之HTML基础知识概述

    1.什么是HTML HTML:Hyper Text Markup Language :超文本标记语言. 超文本:功能比普通文本更加强大. 标记语言:使用一组标签对内容进行描述的语言,它不是编程语言. ...

  10. 【week8】psp~~进度条

    本周psp 项目 内容 开始时间 结束时间 中断时间 净时间 10月7日 星期一 论文 看生物信息方面的论文 10:00 12:00 5 115 写代码 注册信息从前台传入servlet 18:00 ...