Cod sursa(job #664498)

Utilizator ghegoiu1Ghegoiu Stefan ghegoiu1 Data 20 ianuarie 2012 10:39:09
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <algorithm>

using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[300000],n,i,m,t,y,x;
int main()
{
    f>>n;
    for (i=1;i<=n;i++)
        f>>a[i];
    sort(a+1,a+n+1);
    f>>m;
    for (i=1;i<=m;i++)
        {
            f>>t>>y;
            if (t==0)
                {
                    x=upper_bound(a+1,a+n+1,y)-a-1;
                    if(x<=n&&x>=1&&a[x]==y)
                        g<<x<<'\n';
                    else g<<'-1\n';
                }
            else if(t==1)
                {
                x=lower_bound(a+1,a+n+1,y+1)-a-1;
                g<<x<<'\n';
                }
            else
                {
                x=upper_bound(a+1,a+n+1,y-1)-a;
                g<<x<<'\n';
                }
        }
    return 0;
}