CodeForce_Average Superhero Gang Power(贪心)

news/2024/7/11 5:01:25 标签: 贪心, codeforces

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

 


http://www.niftyadmin.cn/n/1154480.html

相关文章

PL/SQL无法连接ORACLE,提示:ORA-12154: TNS: 无法解析指定的连接标识符 - 解决方案...

刚创建了一个数据库&#xff0c;准备用PL/SQL Developer登录时&#xff0c;出现如下错误信息&#xff0c;有时可能会出现英文提示错误对话框&#xff0c;甚至也有可能不会出现错误对话框&#xff0c;故障现象同样是连接不上ORACLE&#xff0c;多试几次对会弹出如下图的错误对话…

牛客寒假算法基础集训营6_D美食(贪心)

题目链接&#xff1a;https://ac.nowcoder.com/acm/contest/332/D 题目描述&#xff1a; 小B喜欢美食。 现在有n个美食排成一排摆在小B的面前&#xff0c;依次编号为1..n&#xff0c;编号为i的食物大小为 a[i] &#xff0c;即足够小B吃 a[i] 口。 小B每次会吃两口&#xff0c…

SSM框架的整合

在分别学习了Spring&#xff0c;SpringMVC和Mybatis三大框架之后&#xff0c;接下来最重要的就是这三大框架的整合&#xff0c;那话不多说&#xff0c;直接进入正题。 1添加jar包 Spring框架的jar包 SpringMVC框架的jar包 Mybatis框架的jar包 Oracle或者Mysql的驱动包 C3P0的数…

UITableViewCell 添加 checkbox 多选

TableViewCell多选&#xff1b; CheckBox; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier "Cell";UITableViewCell *cell [tableView dequeueReusableCellWithIdent…

01背包模板

上图网址为&#xff1a;http://www.karaffeltut.com/NEWKaraffeltutCom/Knapsack/knapsack.html #include <bits/stdc.h> using namespace std; const int maxn50;//最大物品数 const int maxw100;//最大包容量&#xff08;不是包的容量&#xff09; int v[maxn]{0,3,4…

Swift - Framework的制作与使用教程1(纯Swift实现)

Swift中Framework的制作过程&#xff0c;这个作者的文章很有学习价值 引自 http://www.hangge.com/blog/cache/detail_1425.html 引入第三方库 http://www.hangge.com/blog/cache/detail_1426.html 其他生成库 http://www.cocoachina.com/ios/20141126/10322.html iOS使用动态库…

shell脚本检测tomcat进程占用内存大小

2019独角兽企业重金招聘Python工程师标准>>> 脚本作用&#xff1a;监控运行中的tomcat内存占用大小&#xff0c;当内存超过所定义的最大使用内存时&#xff0c;自动重启tomcat&#xff0c;达到释放内存的效果。 脚本如下 check_tomcat.sh&#xff1a; #!/bin/bash#b…

牛客寒假算法基础集训营1_C小a与星际探索(dp)

题目链接&#xff1a;https://ac.nowcoder.com/acm/contest/317/C 题目描述 小a正在玩一款星际探索游戏&#xff0c;小a需要驾驶着飞船从11号星球出发前往nn号星球。其中每个星球有一个能量指数pp。星球ii能到达星球jj当且仅当pi>pjpi>pj。 同时小a的飞船还有一个耐久度…