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 ...
随机推荐
- H2O Driverless AI
H2O Driverless AI(H2O无驱动人工智能平台)是一个自动化的机器学习平台,它给你一个有着丰富经验的“数据科学家之盒”来完成你的算法. 使AI技术得到大规模应用 各地的企业都意识到人工智 ...
- 【Python进阶】用 Python 统计字数
问题描述: 用 Python 实现函数 count_words(),该函数输入字符串 s 和数字 n,返回 s 中 n 个出现频率最高的单词.返回值是一个元组列表,包含出现次数最高的 n 个单词及其次 ...
- spark的运行方式——转载
本文转载自: spark的运行方式 本文主要讲述运行spark程序的几种方式,包括:本地测试.提交到集群运行.交互式运行 等. 在以下几种执行spark程序的方式中,都请注意master的设 ...
- Java JDK安装及环境配置
转载:https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html 环境变量配置: 系统变量→新建 JAVA_HOME 变量 . 变量 ...
- 按照Right-BICEP要求设计的测试用例
测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...
- UVALive - 6916 Punching Robot Lucas+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/96344 Punching Robot Time Limit: 1000MS64bit IO Format: ...
- iOS- Autolayout自动布局
1.前言 •在iOS程序中,大部分视图控制器都包含了大量的代码用于设置UI布局,设置控件的水平或垂直位置,以确保组件在不同版本的iOS中都能得到合理的布局 •甚至有些程序员希望在不同的设备使用相同的视 ...
- Qt多线程-QtConcurrent并行运算高级API
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QtConcurrent并行运算高级API 本文地址:http://tec ...
- Navicat Premium_11.2.7简体中文版 破解版本 windows版本
亲测可用 自己一直在用的 https://pan.baidu.com/s/1VVKKQoIKVB0BgNXBK4YTrQ
- Cmder命令行工具在Windows系统中的配置
一.Cmder简介 Cmder:一款用于Windows系统中,可增强传统cmd命令行工具的控制台模拟器(类似于Linux系统中的终端控制窗口) 特点: 无需安装,解压即用 可使用较多Linux命令,如 ...