Codeforces 939.E Maximize!
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
题目大意:有一个集合,两种操作: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!的更多相关文章
- CodeForces 939E Maximize
Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...
- Codeforces 939 D Love Rescue
Love Rescue 题意:Valya 和 Tolya 是一对情侣, 他们的T恤和头巾上都有小写字母,但是女朋友嫌弃男朋友上T恤上的字不和她的头巾上的字一样,就很生气, 然后来了一个魔法师, 它可以 ...
- Codeforces 939 时区模拟 三分
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...
- 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 ...
- Codeforces 939E - Maximize!
939E - Maximize! 思路: 贪心:最后的集合是最大值+前k小个 因为平均值时关于k的凹形函数,所以可以用三分求最小值 又因为后面的k肯定比前面的k大,所以又可以双指针 三分: #incl ...
- codeforces 939E Maximize! 双指针(two pointers)
E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
随机推荐
- NO--10今天带大家回忆回忆“闭包”吧!
对于‘闭包,我相信很多人都掉进过这个坑里,也相信很多人没能详细的理解这个问题,今天带大家再次走进闭包: 写这篇文章时的心情是十分忐忑的,因为对于我们今天的主角:闭包,很多小伙伴都写过关于它的文章,相信 ...
- 测试类异常Manual close is not allowed over a Spring managed SqlSession
在用Spring 和mybatis整合的 写测试类的时候报出解决办法:在全局配置文件 class="org.mybatis.spring.SqlSessionTemplate" ...
- Centos7 Zabbix监控部署
Zabbix监控 官方文档 https://www.zabbix.com/documentation/3.4/zh/manual https://www.zabbix.com/documentatio ...
- CS小分队第二阶段冲刺站立会议(5月28日)
昨日成果:昨天对我们的软件的主界面进行了思考,考虑到许多人建议我们团队添加可以自主增加软件快捷键的功能,我对这一想法的可行性和项目总体策划进行评估分析后,决定正式实施:已经完成从电脑上添加文件在我们的 ...
- 处理了一个以前写的java小程序的异常
之前用java做过0-99的数字和英文之间的翻译,输入数字就会翻译成英文,输入英文会翻译成数字,比如输入56 输出fiftysix 输入fiftysix 输出56, 发现这会有一个异常,当输入 ...
- DB2 V9 默认帐户信息和服务启动信息
1 dasusr1 DB2 管理服务器用户是管理DAS(Database Adminitrator Service).要完全适用db2 cc 必须启动DAS.DB2 管理服务器(DAS)响应来自 DB ...
- oracle和DB2的差异
1.简介 当今IT的环境正经历着剧烈的变化,依靠单一的关系型数据库管理系统(RDBMS)管理数据的公司开始逐渐减少.分析家的报告指出 ,今天超过90%的公司都拥有不只一种RDBMS.在现在紧张的经济情 ...
- 《DWZ笔记一》<select>动态联动菜单
联动菜单,即组合框Combo box,在DWZ文档中对组合框combox的是这样描述的: 在传统的select 用class 定义:class=”combox”, html 扩展:保留原有属性name ...
- 第二周:PSP&进度条
PSP: 一.词频统计改进 1.表格: C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(hrs) 学习 <构建之法>.Java 8:46 1 ...
- HDU 2105 The Center of Gravity
http://acm.hdu.edu.cn/showproblem.php?pid=2105 Problem Description Everyone know the story that how ...