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 ...
随机推荐
- Python数据挖掘——基础知识
Python数据挖掘——基础知识 数据挖掘又称从数据中 挖掘知识.知识提取.数据/模式分析 即为:从数据中发现知识的过程 1.数据清理 (消除噪声,删除不一致数据) 2.数据集成 (多种数据源 组合在 ...
- Matplotlib外观和基本配置笔记
title: matplotlib 外观和基本配置笔记 notebook: Python tags:matplotlib --- 参考资料,如何使用matplotlib绘制出数据图形,参考另一篇mat ...
- sqoop导入数据到hive表中的相关操作
1.使用sqoop创建表并且指定对应的hive表中的字段的数据类型,同时指定该表的分区字段名称 sqoop create-hive-table --connect "jdbc:oracle: ...
- Python中的构造函数
Python中的构造函数是__init__函数.在Python中,子类如果定义了构造函数,而没有调用父类的,那么Python不会自动调用,也就是说父类的构造函数不会执行. 比如有test.py的mod ...
- 【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?(转)
摘自https://blog.csdn.net/jiange_zh/article/details/50198097 在算法竞赛中,我们常常需要用到一个“无穷大”的值,对于我来说,大多数时间我会根据具 ...
- 冲刺ing-7
第七次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 Leangoo的看板截图,燃尽图 蔺皓雯 编写博客 蔡晨旸 进行测试 曾茜 界面设计 鲁婧楠 界面前后端 杨池宇 界面前后端 项目的发布说 ...
- dcom初步窥探一
一.问题带入: 从delphi 5开始,有许多人都面临过这样的问题:com应用开发出来并且在本机上运行一切正常,但是一旦分发出去实施远程访问时,就无法正常运行了.我自己有段时间在看到“拒绝访问”错误提 ...
- Fiveplus--王者光耀1
**光耀101** 汇总博客: 关文涛: 博客地址:随笔1 随笔2 杨蓝婷: 博客地址:随笔1 随笔2 蔡雅菁: 博客地址:随笔1 随笔2 黄森敏: 博客地址:随笔1 随笔2 林兴源: 博客地址:随笔 ...
- Java中按值传递与按引用传递的区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- 继承&构造函数
子父类中的构造函数的特点. 在子类构造对象时,发现,访问子类构造函数时,父类也运行了. 为什么呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super(); 子类的实例化过程:子类中所 ...