Cod sursa(job #2171118)

Utilizator cezarus30cezarus30 cezarus30 Data 15 martie 2018 11:17:11
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>

using namespace std;

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

const int L = 16, N = 100001;

int             v[ N ], n;

int caut0( int x )
{
    int             r = 0, pas = 1 << L;
    while( pas != 0 )
    {
        if( r + pas <= n && v[ r + pas ] <= x )
        {
            r += pas;
        }
        pas /= 2;
    }
    if( r == 0 || v[ r ] < x )
        r = -1;
    return r;
}

int caut1( int x )
{
    int             r = 0, pas = 1 << L;
    while( pas != 0 )
    {
        if( r + pas <= n && v[ r + pas ] <= x )
        {
            r += pas;
        }
        pas /= 2;
    }
    return r;
}

int caut2( int x )
{
    int             r = 0, pas = 1 << L;

    while( pas != 0 )
    {
        if( r + pas <= n && !(v[ r + pas ] >= x ) )
            r += pas;
        pas /= 2;
    }
    ++r;
    return r;
}

int main()
{
    int             m = 0, t = 0, x = 0;

    fin >> n;
    for( int i = 1; i <= n; ++i )
    {
        fin >> v[ i ];
    }
    fin >> m;
    for( int i = 0; i < m; ++i )
    {
        fin >> t >> x;
        if( t == 0 )
        {
            fout << caut0( x ) << "\n";
        }
        if( t == 1 )
        {
            fout << caut1( x ) << "\n";
        }
        if( t == 2 )
        {
            fout << caut2( x ) << "\n";
        }
    }
    return 0;
}