Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E
E. Maximize!
time limit per test3 seconds
memory limit per test256 megabytes
Problem Description
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 .
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
6
1 3
2
1 4
2
1 8
2
output
0.0000000000
0.5000000000
3.0000000000
input
4
1 1
1 4
1 5
2
output
2.0000000000
解题心得:
- 题意就是给你n次询问,每次有两种情况,第一种,在一个集合中插入一个不小于集合中任意数的数,第二种就是在集合中选一个子集出来,要求子集的最大值减去子集的平均值最大,输出这个最大值。
- 首先有个贪心的思想,要想最大值减去平均值最大,那么最大值要尽可能的大(尝试一下会发现符合要求的子集中必然包含最大值,也比较容易想明白),然后要平均值尽可能的小,那么假设先选一个子集出来,算出平均值,如果集合中还有比平均值更小的数,那么必然可以将这个数加入子集中拉小平均值。
- 因为插入的数是越来越大的,所以形成的数列是升序,那么在找最小的平均值的时候就可以二分查找,先求出前缀和,每次寻找一个点,看这个点的前缀和算出的平均值是否比下一个数更大。以此来进行二分查找。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6;
ll sum[maxn];
vector <int> ve;
int n;
void get_ans(){
int l = 0,r = ve.size()-2,mid;
double ave;//平均值
while(l < r){
mid = (l + r)/2;
ave = (double )(ve[ve.size()-1] + sum[mid])/(double )(mid+2);
if(ave > ve[mid+1])
l = mid+1;
else
r = mid;
}
ave = (double)(sum[l]+ve[ve.size()-1])/(l+2);
double ans = ve[ve.size()-1] - ave;
printf("%.9f\n",ans);//注意一下精度问题
}
int main(){
scanf("%d",&n);
while(n--){
int a,b;
scanf("%d",&a);
if(a == 1){
scanf("%d",&b);
ve.push_back(b);
sum[ve.size()-1] = sum[ve.size()-2]+b;//得出前缀和
}
else
get_ans();
}
return 0;
}
Codeforces Round #464 (Div. 2) E. Maximize!的更多相关文章
- Codeforces Round #464 (Div. 2) D. Love Rescue
D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...
- Codeforces Round #464 (Div. 2) C. Convenient For Everybody
C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm
B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...
- Codeforces Round #464 (Div. 2) A Determined Cleanup
A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm[盒子装仓鼠/余数]
B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]
A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #464 (Div. 2)
A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...
- Codeforces Round #464 (Div. 2) D题【最小生成树】
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- JDBC让java程序连上数据库(mysql数据库)
一.小论异常: 其实JDK已经提供了一组API让java程序连上数据库,并执行SQL语句,其实说起来也蛮简单的,但是绝对是一个细致活,因为稍不留神,异常就铺天盖地的来了,下面说说这些异常吧(声明一下: ...
- 白话SpringCloud | 第二章:服务注册与发现(Eureka)-上
前言 从本章节开始,正式进入SpringCloud的基础教程.从第一章<什么是SpringCloud>中我们可以知道,一个微服务框架覆盖的东西是很多的,而如何去管理这些服务或者说API接口 ...
- Vmware 可用的激活码
4F207-8HJ1M-WZCP8-000N0-92Q6G 0A6Z5-8H1EJ-WZCL1-PK072-23DJG 0F0Q9-8F38L-RZXT9-4U054-ACW5F JA02E-09H4 ...
- 配置百度云盘python客户端bypy上传备份文件
要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...
- 获取hudson持续构建编译结果的一种方法
作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候使用hudson结合VisualStudio进行持续构建后需要获取持续构建的编译结果,通过编译结果来知道哪些项 ...
- <Android 应用 之路> 天气预报(四)
前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...
- HTML 笔记之 HTML 元素的概念
HTML 不是编程语言,它是一种标记语言 HTML 中常见的几个名词是 元素. 标签(开始标签和结束标签). 属性. 元素内容 这篇文章将要介绍的是 HTML 元素的概念 元素的概念 例子: < ...
- 通过 java的 esl 连接 freeswitch
一.目标修改event_socket配置,使之能够建立远端ESL链接. 二.步骤 1. vim ../autoload_configs/event_socket.conf.xml 2. 默认的监听地址 ...
- tf warning等级
from:http://blog.csdn.net/tsinghuahui/article/details/72938764 tf讨厌的warning 2017-08-03 10:02:52.0990 ...
- hdu-2255 奔小康赚大钱---KM模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2255 题目大意: Problem Description 传说在遥远的地方有一个非常富裕的村落,有一 ...