Cod sursa(job #341362)

Utilizator alexandru92alexandru alexandru92 Data 18 august 2009 11:48:59
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
ifstream in;
ofstream out;
vector<int> v;
vector<int>::const_iterator it;
pair< vector<int>::const_iterator, vector<int>::const_iterator > p;
int main()
{int N,x,q;
    in.open("cautbin.in");
    in>>N;
    while( N-- )
    {
        in>>x;
        v.push_back(x);
    }
    in>>N;
    out.open("cautbin.out");
    while( N-- )
    {
        in>>q>>x;
        p=equal_range( v.begin(), v.end(), x );
        if( p.first != p.second )
        {
            if( q <= 1 ) out<<( p.second-v.begin() );
            else out<<( p.first-v.begin()+1 );
        }
        else {
                if( 0 == q ) out<<"-1";
                else if( 1 == q )
                     {
                         it=lower_bound( v.begin(), v.end(), x );
                         out<<( it-v.begin() );
                     }
                     else if( 2 == q )
                          {
                              it=upper_bound( v.begin(), v.end(), x );
                              out<<( it-v.begin()+1 );
                          }
             }
        out<<'\n';
    }
    return 0;
}