Average Superhero Gang Power
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1≤n≤105, 1≤k≤105, 1≤m≤107) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a1,a2,…,an (1≤ai≤106) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a, and the jury’s answer be b. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6.
Examples
input
2 4 6
4 7
output
11.00000000000000000000
input
4 2 6
1 3 2 3
output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Tips
题意:
给定
n
n
n 个正整数
a
1
,
⋯
 
,
a
n
a_1,\cdots,a_n
a1,⋯,an ,现对这组数进行不超过
m
m
m 次操作,每个数不超过
k
k
k 次。能做的操作包括(1)去掉一个数(保证去掉以后还有数)(2)让一个数字自增一。问经过操作以后这组数能达到的最大算术平均值为多少。
解法:
初看觉得有些复杂。不过再想一想不过也就是暴力。可以枚举去掉数字的个数,然后再注意到,数字要从小的开始一个个去(因为越小的越会降低均值)。而这个过程是可以在
O
(
n
lg
n
)
O(n\lg n)
O(nlgn) 的复杂度内做到的(一次排序加一次遍历),因此完全可行。
Reference Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max std::max
#define min std::min
typedef long long ll;
const int MAXN=2e5+10;
ll n,k,m;
ll sum;
int a[MAXN];
double solve(){
double res=(double)(sum+min(k*n,m))/n;
for (int i=1;i<n;++i){
if (i>m) break;
sum-=a[i-1];
res=max(res,(double)(sum+min(k*(n-i),m-i))/(n-i));
}
return res;
}
int main(){
// freopen("in.txt","r",stdin);
sum=0;
scanf("%lld%lld%lld",&n,&k,&m);
for (int i=0;i<n;++i){
scanf("%d",&a[i]);
sum+=a[i];
}
std::sort(a,a+n);
printf("%lf\n",solve());
return 0;
}