Codeforces Problem/1623/C题解

Solution

  Another binary search

  The only thing you need to take notice of is that you have to do the thing in the follewing order:3->n.But there's a paradox.If you want to know how many stones you want to give to the two heaps before the current heap,you have to know how many stones the two heaps after the current heap will give to the cuurrent heap.

  So change your mind.Start with the last heap.The stones one heap can give away is min(h[i],h1[i]-s),(h[i] means the stones one heap originally has,while h1[i] means the stones one heap has after the two heaps has done their operations.)(s means the required number of each heap).

  The AC code is as follow:

#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define mp make_pair
#define ll long long
int n;
int t;
int h[N];
int h1[N];
bool tell1(int s){
//    cout<<s<<endl;
    int now = n;
    for(int i=1;i<=n;i++) h1[i] = h[i];
    while(now>=3){
        if(h1[now]<s) return false;
        int operation = min(h[now],h1[now]-s)/3;
        h1[now-1]+=operation;
        h1[now-2]+=operation*2;
        now--;
    }
    if(h1[1]<s||h1[2]<s) return false;
    return true;
}
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        int r = 0;
        for(int i=1;i<=n;i++){
            cin>>h[i];
            r = max(r,h[i]);    
        }
        int l = 0;
        while(l<r){
            int mid = (l+r+1)/2;
            if(tell1(mid)){
                l = mid;
            }else{
                r = mid-1;
            }
        }
        cout<<l<<endl;
    }
    return 0;
}

 

上一篇:【luogu U138101】字符串水题(SAM)(树状数组)


下一篇:面向对象编程C++(基本知识点)