C. Balanced Stone Heaps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn heaps of stone. The ii-th heap has hihi stones. You want to change the number of stones in the heap by performing the following process once:
What is the maximum number of stones in the smallest heap after the process?
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1051≤t≤2⋅105). Description of the test cases follows.
The first line of each test case contains a single integer nn (3≤n≤2⋅1053≤n≤2⋅105).
The second lines of each test case contains nn integers h1,h2,h3,…,hnh1,h2,h3,…,hn (1≤hi≤1091≤hi≤109).
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print the maximum number of stones that the smallest heap can contain.
Example
input
Copy
4 4 1 2 10 100 4 100 100 100 1 5 5 1 1 1 8 6 1 2 3 4 5 6
output
Copy
7 1 1 3
Note
In the first test case, the initial heap sizes are [1,2,10,100][1,2,10,100]. We can move the stones as follows.
In the second test case, the last heap is 11, and we can not increase its size.
In the third test case, it is better not to move any stones.
In the last test case, the final achievable configuration of the heaps can be [3,5,3,4,3,3][3,5,3,4,3,3].
------------------------------------------------------------------------------------------------------------------------------
让最小值最大,典型的二分答案,一般分析的话,我们可以假设我们知道了答案=mid
题目是正着往后推的,假设h[3]>mid,自然分一部分到前面,可是到了h[4],h[5],万一分配完之后,h[3]又大于mid了,那岂不麻烦了,为什么会出现这样的糟糕情况?因为我们只能保证前面不对后面产生影响,无法保证后面对前面不产生影响;
所以我们从后往前推。定义一个now数组,h[n]>=mid就往前加,然后以此类推;
需要注意的是,我们求的是最小值最大值,因而当mid确定,我们要尽可能让前面的数字大,这样才尽可能使mid成为最小值,所以我们每次向前送走now-mid个-------对吗?
嘿嘿,还真不对,首先,now[i]仅仅代表了正着全部推完,自己不向前推的值,如果这个值 那真无药可救了,加上后面俩大哥给的,自己还没分给小弟就不够了,说明mid无效 now[i]>mid,难道我们就应该分给小弟now[i]-mid吗??当然不是,你分给小弟的时候你只有a[i]个。 你分多了,大哥给完你你还不够mid,你分少了小弟又不一定满足mid,分多少呢?分min(now[i]-mid,h[i])个!啥意思?分h[i]那说明h[i]# include