There are n heaps of stone. The i-th heap has hi 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 3-rd heap to the n-th heap, in this order.
Let i be the number of the current heap.
You can choose a number d (0≤3⋅d≤hi), move d stones from the i-th heap to the (i−1)-th heap, and 2⋅d stones from the i-th heap to the (i−2)-th heap.
So after that hi is decreased by 3⋅d, hi−1 is increased by d, and hi−2 is increased by 2⋅d.
You can choose different or same d 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 t (1≤t≤2⋅105). Description of the test cases follows.
The first line of each test case contains a single integer n (3≤n≤2⋅105).
The second lines of each test case contains n integers h1,h2,h3,…,hn (1≤hi≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, print the maximum number of stones that the smallest heap can contain.
Example
input
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
7
1
1
3
Note
In the first test case, the initial heap sizes are [1,2,10,100]. We can move the stones as follows.
move 3 stones and 6 from the 3-rd heap to the 2-nd and 1 heap respectively. The heap sizes will be [7,5,1,100];
move 6 stones and 12 stones from the last heap to the 3-rd and 2-nd heap respectively. The heap sizes will be [7,17,7,82].
In the second test case, the last heap is 1, 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].
题意:给定一个数组,从第三个数开始,选择一个d,3d<=a[i],然后执行操作:
a[i]-=3d;
a[i-1]+=d;
a[i-2]+=d*2;
每一次的d可以不相同,求执行此操作遍历全部数组所能得到的数组的最小值的最大值是多少
思路:用二分枚举出所有答案,然后从后往前遍历原数组,判断执行操作后是否会使得数组元素小于当前的答案,二分完之后即得结果
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200005];
ll b[200005];
ll n;
bool check(ll mid)
{
for(int i=n-1;i>=2;i--)
{
if(b[i]<mid) return false;
ll p=b[i]-mid;
if(p>=a[i]) p=a[i];
p/=3;
b[i-2]+=p*2;
b[i-1]+=p;
}
if(b[0]>=mid&&b[1]>=mid)
return true;
else
return false;
}
void slove()
{
ll mmax=0x3f3f;
cin>>n;
ll q=-1;
for(int i=0;i<n;i++)
{
cin>>a[i];
q=max(a[i],q);
}
ll l=0;
ll r=0x3f3f3f3f;
while(l<=r)
{
ll mid=(l+r)/2;
for(int i=0;i<n;i++)
b[i]=a[i];
if(check(mid)) mmax=mid,l=mid+1;
else r=mid-1;
}
cout<<mmax<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;cin>>t;
while(t--)
{
slove();
}
}