Educational Codeforces Round 126 (Rated for Div. 2) C. Water the Trees

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
typedef long long ll;
using namespace std;
ll meme[300009],len;
bool check(ll j,ll o,ll max1)
{
    for(int i=0;i<len;i++)
    {
        ll res=max1-meme[i];
        if(res/2<=o)
        {
            o-=res/2;
            j-=res%2;
        }
        else
        {
            res-=o*2;
            o=0;
            j-=res;
        }
        if(j<0)
        return false;
    }
    return true;
}
void solve()
{
    cin>>len;
    for(int i=0;i<len;i++)
    cin>>meme[i];
    sort(meme,meme+len);
    ll max1=meme[len-1];
    ll l=0,r=1e18,ans;
    while(l<=r){
        ll mid=(l+r)>>1;
        if(check(mid-mid/2,mid/2,max1)||check(mid-mid/2,mid/2,max1+1))
            ans=mid,r=mid-1;
           else
            l=mid+1;
    }
    cout<<ans<<endl;
}
int main()
{
    int sum;cin>>sum;
    while(sum--)
    {
        solve();
    }
    return 0;
}

很精妙的二分,让我无话可说

 

posted on 2022-04-14 16:01  zesure  阅读(43)  评论(0编辑  收藏  举报

导航