题目链接:http://codeforces.com/contest/1111/problem/B
(第一次打codeforce,AC的代码被人hack了,大年三十都不让人好过T_T)
题目:
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 nn superheroes in avengers team having powers 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 k operations. Also, on a particular superhero at most kk operations can be done.
Can you help the avengers team to maximize the average power of their crew?
题目大意:对于一群超级英雄,他们分别有最初的力量值,有两种操作:1、给任意英雄力量加一,2、去掉任意一个英雄。求操作后英雄们平均力量的最大值。共有n个超级英雄,每个人最多操作k次(即力量最多加k),总可操作次数为m。
思路:先初始化总力量sum和平均力量ans=(sum+min(k*n,m))/n; 因为n个人每人操作k次的总数有可能超过m,所以是min(k*n,m),然后力量从小到大排序,每次减掉一个人,依次比较上次的ans和新的ans2,取大值。对了还要注意数据范围。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
double a[maxn];
int main()
{
ll n,k,m;
double sum=0;
cin>>n>>k>>m;
for(int i=1;i<=n;i++){
cin>>a[i];sum+=a[i];
}
sort(a+1,a+n+1);
double ans=(sum+min(k*n,m))/n;
for(int i=0;i<n&&i<=m;i++){
sum-=a[i];
double ans2=(sum+min(k*(n-i),m-i))/(n-i);
ans=max(ans,ans2);
}
printf("%.20f\n",ans);
}