Cod sursa(job #2260066)

Utilizator Mgp2005Mgp06Pavel Mihai George Mgp2005Mgp06 Data 14 octombrie 2018 13:16:11
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>

using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100001];
int main() {
    int n,c,x,m,d,s,mij,poz;
    f>>n;
    for (int i=1; i<=n; i++) {
        f>>v[i];
    }
    f>>m;
    for (int i=1; i<=m; i++) {
        f>>c>>x;
        s=1;
        d=n;
        poz=0;
        while (s<d && poz==0)
{
    mij=(d+s)/2;
    if (v[mij]==x) poz=mij;
    else
    if(v[mij]<x)
    {
       s=mij+1;
    }else
    {
        d=mij-1;

    }
}
        if (c==0) {
if (v[poz]==x)
{
    while (v[poz+1]==x)
    {
        poz++;
    }
}
            if (poz==0) {
                g<<-1<<'\n';
            } else {
                g<<poz<<'\n';
            }

        }
        if (c==1) {
if (poz==0) poz=s;


if (v[poz]==x)
{
    poz++;
    while (v[poz]==x)
        poz++;

}
    g<<poz-1<<'\n';

        }
        if (c==2)
        {
if (poz==0) poz=s;

if (v[poz]==x)
{
   poz--;
   while (v[poz-1]==x)
   {
       poz--;
   }
}
    g<<poz<<'\n';
        }
    }
    return 0;
}