目录
原文
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 nn 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⋅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≤105) — the number of players.
The second line of the test case contains a sequence of integers a1,a2,…,an (0≤ai≤109), where aiai 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 105.
Output
For each test case print a single integer — the answer to the problem.
Example
input
4 4 2 3 3 2 3 1 5 2 2 0 0 4 1000000000 1000000000 1000000000 1000000000
output
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.
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→3→2→1
In the third example, there were no passes, so 00 balls are possible.
思路:
是能想到,老是遇到前缀和
1,开始时,我按当前数和之前的前缀和的差值算,一以内不管,但是3,1,3就不对了,而且,管不到整体.
2,求最大:影响球的差值, 求和:作为可承受的极限
3,三种情况:max=0,不用球;sum-max>=max,可承受,1个球;max-(sum-max),需要球.
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj = 1e5+100;
int a[maxj],sum[maxj];
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int mx=0,sum=0;
for(int i=1;i<=n;++i){
cin>>a[i];
mx=max(mx,a[i]);
sum+=a[i];
}
if(mx==0)cout<<0;
else if(sum-mx>=mx)cout<<1;//能否满足最大,能其余小的打打就结束了
else cout<<mx-(sum-mx);//满足不了,差的补上
cout<<'\n';
}
return 0 ;
}