zoukankan      html  css  js  c++  java
  • Codeforces 939 E Maximize!

    题目描述

    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)max(s) means maximum value of elements in ss ,  — the average value of numbers in s . Output this maximum possible value of .

    输入输出格式

    输入格式:

    The first line contains a single integer Q ( 1<=Q<=5·10^{5}1<=Q<=5105 ) — 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<=10^{9}1<=x<=109 ) is a number that you should add to S . It's guaranteed that 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}106 .

    Formally, let your answer be aa , and the jury's answer be bb . Your answer is considered correct if .

    输入输出样例

    输入样例#1: 
    6
    1 3
    2
    1 4
    2
    1 8
    2
    
    输出样例#1: 
    0.0000000000
    0.5000000000
    3.0000000000
    
    输入样例#2: 
    4
    1 1
    1 4
    1 5
    2
    
    输出样例#2: 
    2.0000000000


    首先一定要选最大的元素。
    为什么呢?
    假设有一个最优方案没有最大的元素,那么我们把这个方案里的最大元素替换成当前最大值,
    那么max*num-tot一定会增加(前一项增加(now_max-pre_max)*num,后一项增加now_max-pre_max,又因为num>=1)
    答案又是(max*num-tot)/num,所以嘛、、、、

    然后我是二分的斜率,反正是个离散函数,定义域只有正整数。

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    #define ll long long
    #define maxn 500005
    using namespace std;
    int n,Q,opt,now;
    int l,r,mid,an;
    ll num[maxn];
    double ans=0;
    
    inline double f(int x){
    	return (num[x]+(ll)now)/(double)(x+1);
    }
    
    int main(){
    	scanf("%d",&Q);
    	while(Q--){
    		scanf("%d",&opt);
    		if(opt==1){
    			n++;
    			scanf("%lld",num+n);
    			now=num[n];
    			num[n]+=num[n-1];
    		}
    		else{
    			l=1,r=n-1,an=0;
    			while(l<=r){
    				mid=l+r>>1;
    				if(f(mid)-f(mid-1)<0) an=mid,l=mid+1;
    				else r=mid-1;
    			}
    			
    			ans=now-f(an);
    			printf("%lf
    ",ans);
    		}
    	}
    	
    	return 0;
    }
    

      

     
     
  • 相关阅读:
    App架构经验总结
    通过C语言程序改动控制台的背景和前景颜色
    java实现读取yaml文件,并获取值
    java 实现yaml 数据转json与map
    yaml标记语言的简介
    重新学习之spring第二个程序,配置AOP面向切面编程
    重新学习之spring第一个程序,配置IOC容器
    重新学习Spring之核心IOC容器的底层原理
    hibernate之xml映射文件关系维护,懒加载,级联
    hibernate映射xml文件配置之一对多,多对多
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8475547.html
Copyright © 2011-2022 走看看