Codeforces Codeforces Round #367 (Div. 2) B. Interesting drink

// Accepted Code Link: B. Interesting drink

// Mnaual

//#include<stdio.h>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define high 1000010

typedef long long LL;
typedef vector<LL>vll;

vll ar;

//upper bound
LL khoj(LL lo, LL hi, LL itm)
{
    LL mid;

    while(lo <= hi)
    {
        mid = (lo + hi) / 2;

        if(ar[mid] == itm) lo = mid+1;
        else if(ar[mid] > itm) hi = mid - 1;
        else if(ar[mid] < itm) lo=mid+1;
    }

    return lo;
}

int main()
{
    fast;
    LL n;

    while(cin >> n)
    {
        ar.clear();
        LL i=0 , x;
        for(i=0; i<n; i++)
        {
            cin >> x;
            ar.push_back(x);
        }

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

        //for(i=1; i<len; i++) cout << ar[i] << " ";
        LL q, m;
        cin >> q;
        while(q--)
        {
            cin >> m;

            LL r = khoj(0, ar.size()-1, m);
            //cout << r << " " << ar[r] << "\n";
            outn(r);
        }
    }

    return 0;
}

Comments

Popular posts from this blog

LeetCode Palindrome Number

SPOJ-CMG - Collecting Mango

UVa 929 - Number Maze