CF2014C Robin Hood in Town 题解

题目描述

In Sherwood, we judge a man not by his wealth, but by his merit.

Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood!

There are n people living in the town. Just now, the wealth of the i -th person was ai​ gold. But guess what? The richest person has found an extra pot of gold!

More formally, find an aj​=max(a1​,a2​,…,an​) , change aj​ to aj​+x , where x is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.

A person is unhappy if their wealth is strictly less than half of the average wealth ∗ .

If strictly more than half of the total population n are unhappy, Robin Hood will appear by popular demand.

Determine the minimum value of x for Robin Hood to appear, or output −1 if it is impossible.

∗ The average wealth is defined as the total wealth divided by the total population n , that is, n∑ai​​ , the result is a real number.

输入格式

The first line of input contains one integer t ( 1≤t≤104 ) — the number of test cases.

The first line of each test case contains an integer n ( 1≤n≤2⋅105 ) — the total population.

The second line of each test case contains n integers a1​,a2​,…,an​ ( 1≤ai​≤106 ) — the wealth of each person.

It is guaranteed that the sum of n across all test cases does not exceed 2⋅105 .

输出格式

For each test case, output one integer — the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output −1 instead.

题意翻译

在舍伍德,我们评判一个人不是看他的财富,而是看他的功绩。

环顾四周,富人越来越富,穷人越来越穷。我们需要取富济贫。我们需要罗宾汉

镇上住着 n 人。刚才, i 每个人的财富是 ai​ 金子。但你猜怎么着?最富有的人又找到了一罐金子

更正式地说,找到一个 aj​=max(a1​,a2​,…,an​) ,把 aj​ 改成 aj​+x ,其中 x 是在锅里找到的黄金的非负整数。如果有多个最大值,则可以是其中任意一个。

如果一个人的财富**严格少于平均财富 ∗ 的一半,那么这个人就是不快乐的。

如果**严格来说超过总人口 n 的一半,那么罗宾汉就会应大众的要求出现。

求罗宾汉出现的最小值 x ,如果不可能,则输出 −1 。

∗ 平均财富的定义是总财富除以总人口 n ,即 n∑ai​​ ,结果为实数 。

输入

第一行输入包含一个整数 t ( 1≤t≤104 ) - 测试用例数。

每个测试用例的第一行包含一个整数 n ( 1≤n≤2⋅105 ) - 总人数。

每个测试用例的第二行包含 n 个整数 a1​,a2​,…,an​ ( 1≤ai​≤106 ) --每个人的财富。

保证所有测试用例中 n 的总和不超过 2⋅105 。

输出

对于每个测试用例,输出一个整数 - 最富有的人必须找到的最少黄金数量,罗宾汉才会出现。如果不可能,则输出 −1 。

在第一个测试案例中,不可能只有一个人不快乐。

在第二种情况下,总有 1 个快乐的人(最富有的人)。

在第三种情况下,不需要额外的黄金,所以答案是 0 。

在第四种测试情形中,增加 15 黄金后,平均财富变为 425​ ,而这个平均值的一半是 825​ ,结果是 3 人不快乐。

在第五个测试案例中,加入 16 黄金后,平均财富变为 531​ ,导致 3 人不快乐。

输入输出样例

输入 #1复制

6
1
2
2
2 19
3
1 3 20
4
1 2 3 4
5
1 2 3 4 5
6
1 2 1 1 1 25

输出 #1复制

-1
-1
0
15
16
0

说明/提示

In the first test case, it is impossible for a single person to be unhappy.

In the second test case, there is always 1 happy person (the richest).

In the third test case, no additional gold are required, so the answer is 0 .

In the fourth test case, after adding 15 gold, the average wealth becomes 425​ , and half of this average is 825​ , resulting in 3 people being unhappy.

In the fifth test case, after adding 16 gold, the average wealth becomes 531​ , resulting in 3 people being unhappy.

1.当 n<3 也就是时人数 <3 时, x 无解,所以输出负一。

2. 然后排序。

3. x(也就是最富的那个人找到的黄金数)一直到第n/2+1穷的人的黄金数量小于平均黄金数的一半时,就会召唤出罗宾汉。

4. 输出 x 。

代码:


#include <bits/stdc++.h>
using namespace std;
long long a[200005];//不开longlong见祖宗
int main() {
	long long t;
	cin >> t;
	for (long long i = 0; i < t; i++) {
		long long n,c = 0;
		cin >> n;
		for (long long i = 0; i < n; i++) {
			cin >> a[i];
			c += a[i];
		}//输入
		if (n ==1 or n==2) {
			cout << -1 << endl;
			continue;
		}//如果n=1或n=2那么则不可能召唤出罗宾汉
		sort(a, a + n);//排序
		long long ans = a[n / 2] * 2 * n;
		if (c > ans) {
			cout << 0 << endl;
		} else {
			cout << ans - c + 1 << endl;
		}//输出x=多少时,才能召唤罗宾汉
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值