CodeForce_Average Superhero Gang Power(贪心)

题目链接: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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱吃狮子头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值