UVA 10611 The Playboy Chimp Solution

Solving Process:

The problem provides a sorted list of chimps and asks for the first values before and after a given value. This can be easily solved using lower_bound and upper_bound. Upper bound provides the first index whose value is greater than a given value in an array, and lower bound provides the first index whose value is not less than a given value in an array. Therefore we can lower bound to find the first lowest by decrementing the iterator first. 

Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh   

#include <bits/stdc++.h>

using namespace std;

int main()

{

    long long  i,n,j;

    cin>>n;

    long long  a[n+2]= {0};

    for(i=0; i<n; i++)

    {

        cin>>a[i];

    }

    long long m;

    cin>>m;

    long long b[m+2]= {0};

    for(i=0; i<m; i++)

    {

        cin>>b[i];

        long long mn,mx;

        mn=lower_bound(a,a+n,b[i])-a-1;

        mx=upper_bound(a,a+n,b[i])-a;

        if(a[mn]<b[i]&&b[i]<a[mx])

            cout<<a[mn]<<" "<<a[mx]<<endl;

        else  if(a[mn]<b[i]&&a[mx]<=b[i])

            printf("%d X\n",a[mn]);

        else if(a[mn]>=b[i]&&b[i]<a[mx])

            printf("X %d\n",a[mx]);

        else

            printf("X X\n");


    }

}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.