CISTFILL - Fill the Cisterns - SPOJ Solution C++

  Problem Link : CISTFILL 


👉 Hint : Advanced binary search with some maths

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll n;
ll base[50000],ht[50000],wid[50000],depth[50000];
bool f(double h,double v)
{
    double vol=0;
    for(ll i=0;i<n;i++)
    {
        if(base[i]<h)
        {
            if(base[i]+ht[i]<=h)
                vol+=ht[i]*wid[i]*depth[i];
            else
                vol+=(h-base[i])*wid[i]*depth[i];
            
        }
        if(vol>=v)
                return true;
    }
    return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        double high=0,low=0,mid,v,max=0;
        for(ll i=0;i<n;i++)
        {
            cin>>base[i]>>ht[i]>>wid[i]>>depth[i];
            if(base[i]+ht[i]>max)
                max=base[i]+ht[i];
        }
    //  cout<<max<<endl;
        cin>>v;
    //  cout<<high<<endl;
        high=10000000;
        int k=100;
        while(k--)
        {
            mid=low+(high-low)/2;
            if(f(mid,v))
                high=mid;
            else
                low=mid;
    
        }
        cout.precision(2);
        if(mid<=max)
            cout<<fixed<<mid<<endl;
        else
            cout<<"OVERFLOW"<<endl;
    }
    return 0;
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!