题解:CF2014C Robin Hood in Town

CF2014C 题解

题面

原题传送门

题意

城镇里有 n 个人。现在,第 i 个人的财富是 ai 金币。找出 aj=max(a1,a2,,an),将 aj 改为 aj+x,其中 x 是从罐子里找到的非负整数金币。如果有多个最大值,取其中任意一个即可。

如果一个人的财富严格低于平均财富的一半,他们就会感到不幸福。

如果超过一半的人口感到不幸福,罗宾汉就会应大家的要求出现。

请确定为了让罗宾汉出现的最小 x 值,或者输出 1 如果这不可能。

思路

依题求即可。

首先 n1,2 的时候显然无解。

接着想到给 a 排序,并算出 a 的和 cnt,算出第 n2+1 个数,以这个数为平均数,求出有超过一半的人口感到不幸福时钱的总数,减去 cnt 即可。

具体见代码。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int MN=2e5+5;
ll n,a[MN],num,cnt;
void write(ll n){if(n<0){putchar('-');write(-n);return;}if(n>9)write(n/10);putchar(n%10+'0');}
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
void solve(){
    n=read();cnt=0;
    for(int i=1; i<=n; i++) a[i]=read(),cnt+=a[i];
    if(n==1||n==2){
        write(-1);putchar('\n');
        return;
    }
    sort(a+1,a+1+n);
    num=a[(n>>1)+1];
    num=num*2*n+1;
    write(max(0ll,num-cnt));putchar('\n');
}
int main(){
    // freopen("1.in","r",stdin);
    ll T=read();while(T--) solve();
	return 0;
}
posted @   naroto2022  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
花开如火,也如寂寞。