... HAVE U TRIED .... AT LEAST 3 TIMES ... OK U CAN SEE THIS ...
Showing posts with label Binary Search. Show all posts
Showing posts with label Binary Search. Show all posts

Monday, January 6, 2014

LOJ-1062 :: Crossed Ladders

//Problem link>>http://lightoj.com/volume_showproblem.php?problem=1062

#include <bits/stdc++.h>
#define eps 1e-7
using namespace std ;

 double x , y ,c ,low ,high ,mid ,p,q ,x1,x2 ;

double call (double n)
{
    p=sqrt(y*y-n*n) ;
    q=sqrt(x*x-n*n) ;

    x1=(n*c)/p ;
    x2=(n*c)/q ;

    return x1+x2 ;
}

int main ()
{
    int t , i ,it=0 ;
    cin>>t ;
    while (it<t)
    {
        it++ ;
        cin>>x>>y>>c ;
        low=0.0 ;
        high=min(x,y)*1.0 ;

         while (fabs(low-high)>eps)
         {
             mid=(low+high)/2.0 ;
             if (call(mid)<mid) low=mid;
             else if (call(mid)>mid) high=mid  ;
         }
         cout<<"Case "<<it<<": " ;
         cout<<setprecision(10)<<low<<endl ;
 
    }

    return 0 ;
}

LOJ-1138 :: Trailing Zeroes (III)

//Problem link>>http://lightoj.com/volume_showproblem.php?problem=1138


#include <bits/stdc++.h>
using namespace std ;

int rez (int x)
{
    int sum=0 ;
    while (x!=0)
    {
        x=x/5 ;
        sum+=x  ;
    }
    return sum ;
}

int n ;
int go ()
{
    int low=0 ,high=1000000000 ,mid ,ans=0 ;
    while (low<=high)
    {
        mid=(low+high)/2 ;
        if (rez(mid) < n) low=mid+1 ;
        else if (rez(mid) > n) high = mid-1 ;
        else
        {
            ans =mid ;
            high=mid-1;
        }
    }
    return ans ;
}

int main ()
{
    int cas, i ,ret ;
    cin>>cas ;
    for (i=1 ;i <=cas ; i++)
    {
        cin>>n ;
         ret=go () ;

        if (ret==0) cout<<"Case "<<i<<": "<<"impossible"<<endl ;
        else cout<<"Case "<<i<<": "<<ret<<endl ;
    }

    return 0 ;
}

Saturday, January 4, 2014

UVA-11413 :: Fill the Containers

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2408

#include <bits/stdc++.h>
using namespace std ;

int ves ,con , i , a[10000]  ;

int call (int n)
{
    int i=1 , k=1 , now=0 ;
    while (k <= con)
    {
        if (i > ves) return 1 ;
        if (now+a[i]<=n)
        {
            now+=a[i] ;
            i++ ;
        }
        else
        {
            now=0 ;
            k++ ;
        }
    }

    return  0 ;
}

int main ()
{
    while (cin>>ves>>con)
    {
        int sum=0 ;
        for (i=1 ; i<=ves ; i++)
        {
            cin>>a[i] ;
            sum=sum+a[i] ;
        }
        int  low=0 , high=sum ,ans=0 ;

        while (low<=high)
        {
           int mid=(low+high)/2 ;

            if (call(mid) == 0) low=mid+1 ;
            else
            {
                ans=mid ;
                high=mid-1 ;
            }
        }
        cout<<ans<<endl ;
    }

    return 0 ;
}