Mai intai trebuie sa te autentifici.

Cod sursa(job #2072489)

Utilizator ARobertAntohi Robert ARobert Data 21 noiembrie 2017 21:44:10
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 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,poz;

void c0(int x)
{
    s=1;
    d=n;
    poz=-1;
    while (s<=d)
    {
        mij=s+(d-s)/2;
        if (x==t[mij])
            poz=mij;
        if (x>=t[mij])
            s=mij+1;
        else d=mij-1;
    }
    fout<<poz<<endl;
}

void c1(int x)
{
    s=1;
    d=n;
    poz=-1;
    while (s<=d)
    {
        mij=s+(d-s)/2;
        if (x>=t[mij])
            {
                poz=mij;
                s=mij+1;
            }
        else d=mij-1;
    }
    fout<<poz<<endl;
}

void c2(int x)
{
    s=1;
    d=n;
    poz=-1;
    while (s<=d)
    {
        mij=s+(d-s)/2;
        if (x<=t[mij])
            {
                poz=mij;
                d=mij-1;
            }
        else s=mij+1;
    }
    fout<<poz<<endl;
}



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)
        c0(x);
        else if (tip==1)
        c1(x);
        else
        c2(x);
    }
    return 0;
}