Cod sursa(job #2072458)

Utilizator ARobertAntohi Robert ARobert Data 21 noiembrie 2017 21:15:38
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,i,t[100001], s,d,mij,x,tip;

int main()
{
    fin>>n;
    for (i=1;i<=n;i++)
        fin>>t[i];
    fin>>m;
    for (i=1;i<=m;i++)
    {
        fin>>tip>>x;
        if (tip==0)
        {
            s=1;
            d=n;
            int poz=-1;
            while (s<=d)
            {
                if (x==t[m])
                    poz=m;
                    if (x>=t[m])
                        s=m+1;
                    else d=m-1;
            }
            fout<<poz<<endl;
        }
        else if (tip==1)
        {
            s=1;
            d=n;
            int poz=-1;
            while (s<=d)
            {
                m=(s+d)/2;
                if (x<=t[m])
                    {
                        poz=m;
                        s=m+1;
                    }
                    else d=m-1;
            }
            fout<<poz<<endl;
        }
        else
        {
            s=1;
            d=n;
            int poz=-1;
            while (s<=d)
            {
                m=(s+d)/2;
                if (t[m]>=x)
                    {
                        poz=m;
                        s=m-1;
                    }
                    else d=m+1;
            }
            fout<<poz<<endl;
        }
    }
    return 0;
}