There are nn trees in a park, numbered from 11 to nn. The initial height of the ii-th tree is hihi.
You want to water these trees, so they all grow to the same height.
The watering process goes as follows. You start watering trees at day 11. During the jj-th day you can:
- Choose a tree and water it. If the day is odd (e.g. 1,3,5,7,…1,3,5,7,…), then the height of the tree increases by 11. If the day is even (e.g. 2,4,6,8,…2,4,6,8,…), then the height of the tree increases by 22.
- Or skip a day without watering any tree.
Note that you can't water more than one tree in a day.
Your task is to determine the minimum number of days required to water the trees so they grow to the same height.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases.
The first line of the test case contains one integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of trees.
The second line of the test case contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the height of the ii-th tree.
It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105 (∑n≤3⋅105∑n≤3⋅105).
Output
For each test case, print one integer — the minimum number of days required to water the trees, so they grow to the same height.
Example
input
Copy
3 3 1 2 4 5 4 4 3 5 5 7 2 5 4 8 3 7 4
output
Copy
4 3 16
Note
Consider the first test case of the example. The initial state of the trees is [1,2,4][1,2,4].
- During the first day, let's water the first tree, so the sequence of heights becomes [2,2,4][2,2,4];
- during the second day, let's water the second tree, so the sequence of heights becomes [2,4,4][2,4,4];
- let's skip the third day;
- during the fourth day, let's water the first tree, so the sequence of heights becomes [4,4,4][4,4,4].
Thus, the answer is 44.
思路:
1,二分是为了优化
2,天,肯定是一奇一偶,奇数天可多一
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=3e5+100,mod=1e9+7,mm=4e4+10;
#define sss fixed<<setprecision(15)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
bool cmp(int a,int b){
return a > b;
}
int a[maxj],b[maxj],n;
bool check(int x){
int num2=x/2;//偶数天的个数
int num1=x-num2;
for(int i=1;i<=n;++i){
int num=min(num2,b[i]/2);//优先偶数天
num2-=num;
num1-=b[i]-num*2;
if(num1<0||num2<0)return 0;//天数消耗光了,就是不够
}
return 1;
}
void solve(){
cin>>n;int Mx=-1;
for(int i=1;i<=n;++i)cin>>a[i],Mx=max(Mx,a[i]);
int Ans=1e18;
for(int i=0;i<=1;++i){
int mx=Mx+i;
int l=0,r=1e18;
for(int i=1;i<=n;++i){
b[i]=mx-a[i];
}int ans=1e18;
while(l<=r){
int mid=(l+r)>>1;//找的是最小天数
if(check(mid)){
r=mid-1;
ans=mid;
}else{
l=mid+1;
}
}
Ans=min(Ans,ans);
}
cout<<Ans<<'\n';
}
int32_t main() {//二分找,具有单调性,(最大中最小,最小中最大),又因为复杂度太高,需要二分搜索
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}