Cod sursa(job #668673)

Utilizator feelshiftFeelshift feelshift Data 25 ianuarie 2012 13:48:51
Problema Cautare binara Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
// http://infoarena.ro/problema/cautbin
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXSIZE = 100001;

ifstream in("cautbin.in");
ofstream out("cautbin.out");

int length,questions;
vector<int> v;

int main()
{
    in >> length;
    for(int i=1;i<=length;i++)
    {
        in >> questions;    // tmp variable
        v.push_back(questions);
    }

    in >> questions;

    int type,value;
    vector<int>::iterator it;

    for(int i=1;i<=questions;i++)
    {
        in >> type >> value;

        switch(type)
        {
            case 0:
                {
                    it = upper_bound(v.begin(),v.end(),value);
                    if(it == v.end())
                    {
                        if(*v.rbegin() == value)
                            out << v.size();
                        else
                            out << "-1";
                    }
                    else
                    {
                        if(it != v.begin())
                            --it;

                        if(*it == value)
                            out << (it - v.begin()) + 1 << "\n";
                        else
                            out << "-1\n";
                    }
                } break;
            case 1:
                {
                    it = upper_bound(v.begin(),v.end(),value);
                    //if(it != v.begin());
                      //  --it;

                    out << (it - v.begin()) << "\n";
                } break;
            case 2:
                {
                    it = lower_bound(v.begin(),v.end(),value);

                    out << (it - v.begin()) + 1 << "\n";
                } break;
        }
    }

    return (0);
}