其他分享
首页 > 其他分享> > CodeForce div2 Round #763

CodeForce div2 Round #763

作者:互联网

C-Balanced Stone Heaps

#include<bits/stdc++.h>
using namespace std;
const int maxn =2e5+5;
#define ll long long
// 二分答案
// 如何进行检验?
// 检验方法是本道题的关键

int T,n;
ll a[maxn];

int check(int x){
    int flag=0;
    vector<int> b(n+10);
    // 本题卡memset,
    for(int i=n;i>=3;i--){
        if(b[i]>=x){
            ll t=a[i]/3;
            b[i-1]+=t;
            b[i-2]+=2*t;
        }else if(a[i]+b[i]>=x){
            ll t=(a[i]-(x-b[i]))/3;
            b[i-1]+=t;
            b[i-2]+=2*t;
        }else{
            flag=1;
            break;
        }
    }
    if(a[1]+b[1]<x||a[2]+b[2]<x)flag=1;
    return !flag;

}

int main(){
    cin>>T;
    while(T--){
        cin>>n;
        ll minx=1e9,maxx=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            minx=min(minx,a[i]);
            maxx=max(maxx,a[i]);
        }
        ll l=minx,r=maxx;
        ll ans;
        while(l<=r){
            ll mid=(l+r)>>1;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else r=mid-1;
        }
        cout<<ans<<endl;
    }

    return 0;
}

标签:le,int,763,ll,mid,枚举,CodeForce,答案,Round
来源: https://www.cnblogs.com/Ghaser/p/15763639.html