Robin Hood in Town

C. Robin Hood in Town

Problem

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 n n people living in the town. Just now, the wealth of the i i i-th person was a i a_i ai gold. But guess what? The richest person has found an extra pot of gold!

More formally, find an a j = m a x ( a 1 , a 2 , … , a n ) a_j=max(a_1, a_2, \dots, a_n) aj=max(a1,a2,,an), change a j a_j aj to a j + x a_j+x aj+x, where x x 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 ∗ ^{\text{∗}} .

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

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

∗ ^{\text{∗}} The average wealth is defined as the total wealth divided by the total population n n n, that is, ∑ a i n \frac{\sum a_i}{n} nai, the result is a real number.

Input

The first line of input contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2\cdot10^5 1n2105) — the total population.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 6 1 \le a_i \le 10^6 1ai106) — the wealth of each person.

It is guaranteed that the sum of n n n across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

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

Example

Input
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
Output
-1
-1
0
15
16
0

Note

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

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

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

In the fourth test case, after adding 15 15 15 gold, the average wealth becomes 25 4 \frac{25}{4} 425, and half of this average is 25 8 \frac{25}{8} 825, resulting in 3 3 3 people being unhappy.

In the fifth test case, after adding 16 16 16 gold, the average wealth becomes 31 5 \frac{31}{5} 531, resulting in 3 3 3 people being unhappy.

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll N=2e5+10;
ll a[N];

int main()
{
    ll t;
    cin>>t;

    while(t--)
    {
        ll n;
        cin>>n;

        ll num=0;
        for(ll i=0;i<n;i++)
        {
            cin>>a[i];
            num+=a[i];
        }

        if(n==1||n==2)
        {
            cout<<-1<<endl;
            continue;
        }
        
        sort(a,a+n);
        
        ll ans=a[n/2]*2*n;
        if(num>ans) cout<<0<<endl;
        else cout<<ans-num+1<<endl;
    }
    
    return 0;
}
### 回答1: robin_hood::unordered_set是一种基于开放寻址的哈希表实现,它是C++ STL中的一个无序集合容器。与标准的unordered_set相比,robin_hood::unordered_set有着更高的性能。 robin_hood::unordered_set的实现方式采用了"robin hood"哈希算法,这种算法通过再哈希的方式处理冲突,将冲突的元素移到更远的位置,从而保持高效的查找性能。这种算法使得插入和删除操作具有O(1)的时间复杂度,而查找操作虽然在最坏情况下也是O(n),但实际上在大多数情况下是O(1)的。 此外,robin_hood::unordered_set在内存使用上也比标准的unordered_set更为高效。它采用了连续的内存布局,并使用了布隆过滤器来减少哈希冲突的数量,从而减少了内存的占用。 使用robin_hood::unordered_set时,可以通过插入、删除和查找等操作来管理集合中的元素。插入操作可以将元素添加到集合中,删除操作可以从集合中移除指定的元素,而查找操作可以判断集合中是否存在某个元素。 总的来说,robin_hood::unordered_set是一种高效的无序集合容器,适用于需要频繁进行插入、删除和查找操作的场景。它通过"robin hood"哈希算法和优化的内存使用方式,在性能和内存占用方面均有优势。 ### 回答2: Robin Hood是一个著名的英雄人物,他以偷取富人财物来帮助穷人而闻名。而unordered_set是C++ STL库中的一个数据结构,它是一个无序的集合,允许快速地插入、查找和删除元素。 尽管二者似乎没有直接联系,但是我们可以通过一些类比来理解它们之间的关系。就像Robin Hood通过偷取富人的财物来帮助穷人一样,unordered_set可以用来解决一些问题,比如查找和删除元素,这些问题在其他数据结构中可能需要更多的时间和资源。 就像Robin Hood能够迅速地从富人身上夺取财物,unordered_set在最佳情况下能够以O(1)的时间复杂度插入、查找和删除元素,这取决于哈希函数的性能。这使得它在一些需要高效率操作的场景中非常有用,比如去重、查找等。 然而,就像Robin Hood有时候可能会遇到困难一样,unordered_set也有一些限制。由于其无序的特点,它在有序访问元素方面相对较弱。此外,当元素数量较大时,哈希冲突的概率也会增加,导致性能下降。因此,在某些情况下,我们可能需要考虑使用其他更适合的数据结构。 总之,尽管Robin Hood和unordered_set在本质上是不同的,但通过类比,我们可以更好地理解unordered_set的特点和用途。无论是Robin Hood还是unordered_set,它们都有自己独特的功能和限制,我们需要根据具体的问题和需求来选择使用它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Pretty Boy Fox

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

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

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

打赏作者

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

抵扣说明:

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

余额充值