C. Balanced Stone Heaps- Codeforces Round #763 (Div. 2)

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:

  • You go through the heaps from the 33-rd heap to the nn-th heap, in this order.
  • Let ii be the number of the current heap.
  • You can choose a number dd (0≤3⋅d≤hi0≤3⋅d≤hi), move dd stones from the ii-th heap to the (i−1)(i−1)-th heap, and 2⋅d2⋅d stones from the ii-th heap to the (i−2)(i−2)-th heap.
  • So after that hihi is decreased by 3⋅d3⋅d, hi−1hi−1 is increased by dd, and hi−2hi−2 is increased by 2⋅d2⋅d.
  • You can choose different or same dd for different operations. Some heaps may become empty, but they still count as heaps.

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.

  • move 33 stones and 66 from the 33-rd heap to the 22-nd and 11 heap respectively. The heap sizes will be [7,5,1,100][7,5,1,100];
  • move 66 stones and 1212 stones from the last heap to the 33-rd and 22-nd heap respectively. The heap sizes will be [7,17,7,82][7,17,7,82].

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]now[i]-mid, 你分now[i]-(now[i]-mid)个时恰好=mid,再多分你自己就不够了。

# include 
#include
# include
# include
# include
using namespace std;
typedef long long int ll;
ll  h[100000*2+10], now[100000*2+10];
int n;
bool pan(ll mid)
{


    for(int i=1; i<=n; i++)
        now[i]=h[i];

    for(int i=n; i>=3; i--)
    {
        if(now[i]=mid&&now[2]>=mid)
        return 1;
    return 0;
}
int main()
{

    int t;
    cin>>t;

    while(t--)
    {

        cin>>n;
        ll left=0;
        ll right=0;

        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&h[i]);
            right=max(right,h[i]);


        }

        ll mid=(right+left)/2;

        while(left<=right)
        {
            mid=(right+left)/2;

            if(pan(mid))
                left=mid+1;
            else
                right=mid-1;

        }

        cout<

你可能感兴趣的:(c语言,p2p,开发语言)