思路:二分答案,对于二分值h,判断其是否满足条件,可遍历a[i],对于小于等于h,sum-=(h-a[i])*2,大于h的,则需要对其奇偶判断,因为每次只能减2,因此只能为偶数,即将多余的1去掉即可
Code :
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX_N=300005;
int n,T;
int a[MAX_N];
bool Find(int x);
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;++i)
cin>>a[i];
sort(a,a+n);
int l=a[0],r=a[n-1];
while(l<=r){
int h=(l+r)>>1;
if(Find(h)==true) l=h+1;
else r=h-1;
}
cout<<r<<endl;
}
return 0;
}
bool Find(int x)
{
bool boo=false;
LL sum=0;
for(int i=0;i<n;++i)
if(a[i]<x) sum-=(x-a[i])*2;
else{
int t=a[i]-x;
t-=t%2;
sum+=t;
}
if(sum>=0) boo=true;
return boo;
}