Cod sursa(job #1733400)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 24 iulie 2016 17:06:34
Problema Cautare binara Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>

using namespace std;

long n,m,v[100002],y=0;

ifstream f ("cautbin.in");
ofstream t ("cautbin.out");
ifstream u ("cautbin.ok");

long binary_search_up(long val)
{long i, step;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n and v[i + step] <= val)
i += step;
return i+1;
}

long binary_search_low(long val)
{long i, step;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n and v[i + step] < val)
i += step;
return i+2;
}

void check(long solution){long response;
u>>response;
if (solution!=response)
cout<<"Error: "<<y<<'\n';
++y;
}

int main()
{long q,x;
f>>n;
for (long i=0;i<n;++i)
f>>v[i];
f>>m;
for (long i=0;i<m;++i){
f>>q>>x;
if (q==1)
t<<binary_search_up(x)<<'\n';
else if (q==0){
if (v[binary_search_up(x)]!=x) t<<-1<<'\n';
else t<<binary_search_up(x);}
else if (q==2) t<<binary_search_low(x)<<'\n';
}
return 0;
}