B. Game of Ball Passing
time limit per test
1 second
memory limit per test
256 megabytes
Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.
The game involves n players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.
Find the minimum possible amount of balls that were involved in the game.
Input
There are several test cases in the input data. The first line contains a single integer t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 ) t (1 \leq t \leq 5 \cdot 10^4) t(1≤t≤5⋅104) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer n 2 ≤ n ≤ 1 0 5 2 \leq n \leq 10^5 2≤n≤105 — the number of players.
The second line of the test case contains a sequence of integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 1 0 9 ) a_1, a_2, \ldots, a_n (0 \leq a_i \leq 10^9) a1,a2,…,an(0≤ai≤109)where a i a_i ai is the number of passes delivered by the i-th player.
It is guaranteed that the sum of n over all test cases doesn’t exceed 1 0 5 10^5 105
Output
For each test case print a single integer — the answer to the problem.
Example
input
Copy
4
4
2 3 3 2
3
1 5 2
2
0 0
4
1000000000 1000000000 1000000000 1000000000
output
Copy
1
2
0
1
Note
In the first test case, with the only ball, the game can go like this:
2 → 1 → 3 → 4 → 1 → 2 → 3 → 4 → 2 → 3 → 2. 2 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2. 2→1→3→4→1→2→3→4→2→3→2.
In the second test case, there is no possible way to play the game with only one ball. One possible way to play with two balls:
2 → 1 → 2 → 3 → 2 → 1. 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1. 2→1→2→3→2→1.
2 → 3 → 2 → 1 2 \rightarrow 3 \rightarrow 2 \rightarrow 1 2→3→2→1
In the third example, there were no passes, so 0 balls are possible.
这道题…不好想,但是原理实在太简单了,你可以假设只有一个人的情况,和2个人的情况,一个人的话,有多少就是多少个球,两个人的话,你会发现,两个人相同的时候或者是一个人比另一个人大一个球的时候就是1个球,不相同且相差大于1的时候一定是一个球加是另一个人多出来的那一部分。
推广到3个人,你可以想象其实是一个人轮流和别人踢,也就是很多次2个人的情况,最好选次数最多的人来轮,如果次数最多的人刚好等于剩下的人加起来的,那就是一个球的事,如果这个人大于呢?那最后剩下的只能自己踢了,小于呢 ,这个时候就在剩下部分传球,不管最大的max,你会发现无论怎样,剩下部分传球都会到等于最大(sum=max)的情况。所以只要剩下的和(sum)大于最大max,他就能化为相等的情况,所以sum>=max就是一个球,sum<max就是sum-max个球。
/*********************************************************************
程序名:775B
日期: 2022-03-28 20:48
*********************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int t;
while (n--) {
cin >> t;
ll tmp;
ll maxn = 0, sum = 0;
for (int i = 1; i <= t; i++) {
cin >> tmp;
sum += tmp;
maxn = max(maxn, tmp);
}
if (!sum) {
cout << "0" << endl;
continue;
}
sum -= maxn;
if (sum >= maxn)
cout << "1" << endl;
else
cout << maxn - sum << endl;
}
return 0;
}