Codeforces1111B 思维

题意:给你 n,k,m三个值 a1~an n个值
让你总共操作m次 每次有两种选择:1 去掉一个元素 2 给某个元素+1
且每个元素最多加k次
问你最后剩下元素的平均值最大是多少。
思路:首先感谢队友振国gg提供了“最大值的个数”这个概念让我有了思路
先找到最大值的数量。然后排个序处理一下前缀和(方便后面运算)
然后有一点要明白:一共n个数 如果选择去掉i个 那剩下m-i可以加上 那相当于总平均值加了(m-i)/(n-i) 意思是 删去不同数量的元素 剩下的m-i 可以使结果增加的权值不同
然后大体分两种情况:
1、m<=非最大值的数量p 就是说无法减去所有比最大值小的数 比如数列是 0 0 0 0 1 m是3 此时非最大值的数量是4,此时枚举所有可以减去的数量 (0~4) 然后计算平均值 取最大的 //这里不用考虑k因为k>=1 至少每个可加一次嘛
2、m>非最大值的数量p 比如数列是0 0 0 1 1 m是6 此时非最大值的数量是3
这种情况 我们就不需要算出第一种情况 (自己后来才发现的) 因为此时一定是优先删去所有的非最大值(因为可以删完)
只需要看m和n的关系即可
若m<=n 那么m的数量比n少 那直接选择m-i都加上 不删除元素
若m>n 那么加上min(m-(n-1),k)
下面是代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define MID (t[k].l+t[k].r)>>1
#define cl(a,b) memset(a,b,sizeof(a))
#define dbg() printf("aaa\n")
using namespace std;
int n,k,m;
const int maxn=1e5+10;
ll a[maxn];
int main() {
	scanf("%d%d%d",&n,&k,&m);
    rep(i,1,n) scanf("%lld",&a[i]);
    sort(a+1,a+n+1);
    int p=lower_bound(a+1,a+n+1,a[n])-a;
    p--;//现在p是最后一个不是当前的数
    rep(i,2,n) a[i]+=a[i-1];
    //现在a是前缀和
    double maxx=0.0,tp;
    if(m<=p){
        rep(i,0,m){//枚举被去掉的长度i
            tp=(double)(a[n]-a[i]);//tp是剩下的总和
            tp+=(double)(m-i);
            tp/=(double)(n-i);
            if(maxx<tp) maxx=tp;
        }
    }else{//现在m>p
        // 下面这一部分就不用考虑了!!!
        // rep(i,0,p){//枚举被去掉的长度i
        //     tp=(double)(a[n]-a[i]);//tp是剩下的总和
        //     tp+=(double)min((m-i),k*(n-i));
        //     tp/=(double)(n-i);
        //     if(maxx<tp) maxx=tp;
        // }
        if(m-p<=n-p){//若剩m小于剩n 剩下这些都留 全加
            tp=(double)(a[n]-a[p]);
            tp+=(double)(m-p);
            tp/=(double)(n-p);
            if(maxx<tp) maxx=tp;
        }else{//m-p>n-p
            tp=(double)(a[n]-a[n-1]);//这你写错了!!!
            tp+=(double)min(m-(n-1),k);
            if(maxx<tp) maxx=tp;
        }
    }
    printf("%.20lf\n",maxx);
	return 0;
}

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 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.

发布了120 篇原创文章 · 获赞 12 · 访问量 5284

猜你喜欢

转载自blog.csdn.net/weixin_43735161/article/details/104530105
今日推荐