其他分享
首页 > 其他分享> > Codeforces Round #775

Codeforces Round #775

作者:互联网

题目链接

Codeforces Round #775

B. Game of Ball Passing

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\left(1 \leq t \leq 5 \cdot 10^{4}\right)-\) the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer \(n\left(2 \leq n \leq 10^{5}\right)-\) the number of players.
The second line of the test case contains a sequence of integers \(a_{1}, a_{2}, \ldots, a_{n}\left(0 \leq a_{i} \leq 10^{9}\right)\), where \(a_{i}\) 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 \(10^{5}\).

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

解题思路

思维

考虑传球最多的次数 \(mx\),类似于填数:\(——mx——mx——mx—— \dots ——mx\),如果 \(mx\leq sum-mx\),则一定可以将 \(——\)填满,即只需要一个球,反之,尽量填数:\(mx——mx——mx—— \dots ——mx\),填不了则还要形成一条链,共 \(2mx-sum\) 条链,另外考虑全为 \(0\) 的特殊情况

代码

// Problem: B. Game of Ball Passing
// Contest: Codeforces - Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1649/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}


int main()
{
    int t,n;
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	LL mx=0,s=0;
    	while(n--)
    	{
    		LL x;
    		cin>>x;
    		chkMax(mx,x);
    		s+=x;
    	}
    	cout<<(mx?max(1ll,2*mx-s):0)<<'\n';
    }
    return 0;
}

标签:775,10,Codeforces,Round,leq,test,mx,define
来源: https://www.cnblogs.com/zyyun/p/15976097.html