Accepted solution

UVA 10611 - The Playboy Chimp solution

Open original problem
Source code

#include <iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

    int n,m;

    int indexL,indexR;

    cin>>n;

    vector<int>a(n);

    for(int i=0; i<a.size(); i++)

    {

        cin>>a[i];

    }

    sort(a.begin(),a.end());

    cin>>m;

    vector<int>b(m);

    for(int i=0; i<b.size(); i++)

    {

        cin>>b[i];

    }

    for(int i=0; i<b.size(); i++)

    {

        int checke  = b[i];

        indexL = -5;

        int j=0;

        while(a[j]<checke && j<a.size())

        {

            indexL = a[j];

            j++;

        }

        int p = j-1;

        j = a.size()-1;

        while(a[j]>checke && j>p)

        {

            indexR = a[j];

            j--;

        }

        if(j>=a.size()-1 && a[j] <checke)

        {

            indexR = -5;

        }

        if(indexL==-5 && indexR ==-5)

        {

            cout<<"X X"<<endl;

        }

        else if(indexL==-5 )

        {

            cout<<"X "<<indexR<<endl;

        }

        else if(indexR==-5 )

        {

            cout<<indexL<<" X"<<endl;

        }

        else

        {

            cout<<indexL<<" "<<indexR<<endl;

        }

    }

    return 0;

}
Join the discussion

Add your comment