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;
    }
    

      

     
     
  • 相关阅读:
    由jQuery Validation Remote验证引起的错误(MVC3 jQuery.validate.unobtrusive)
    Windows8下设置VS默认启动方式为管理员启动
    Asp.Net MVC 必备插件MVC Route Visualizer(Visual Studio 2012 版)
    2012 LinkCoder Jeffrey Richter:Win 8应用开发与.NET4.5
    WCF应用:宿主与调用纯代码示例(Host &Client code only sample)
    Nexus 7 入手风波记
    [转]使用HyperV BPA(Best Practices Analyzer最佳化分析工具)
    [转]SCVMM2012部署之一:先决条件条件准备
    [转]VMware管理员必掌握的八个HyperV功能
    [转]Installing and Configuring target iSCSI server on Windows Server 2012
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8475547.html
Copyright © 2011-2022 走看看