Cod sursa(job #2281562)
| Utilizator | Data | 12 noiembrie 2018 14:50:22 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.45 kb |
#include <iostream>
#include <fstream>
using namespace std;
int v[100001],n,i,m,q,x,ok,st,dr,mid;
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin>>n;
for (i=1;i<=n;i++)
fin>>v[i];
fin>>m;
for (i=1;i<=m;i++)
{
fin>>q>>x;
if (q==0)
{
ok=0; /// presupun ca x nu se afla in sir
st=1;
dr=n;
while (st <= dr)
{
mid=(st+dr)/2;
if (v[mid]==x)
ok=1;
if (v[mid] <= x)
st=mid+1;
else
dr=mid-1;
}
if (ok==0)
fout<<"-1"<<"\n";
else
fout<<dr<<"\n";
}
if (q==1)
{
st=1;
dr=n;
while (st<=dr)
{
mid=(st+dr)/2;
if (v[mid]<=x)
st=mid+1;
else
dr=mid-1;
}
fout<<dr<<"\n";
}
if (q==2)
{
st=1;
dr=n;
while (st<=dr)
{
mid=(st+dr)/2;
if (v[mid]<x)
st=mid+1;
else
dr=mid-1;
}
fout<<st<<"\n";
}
}
return 0;
}
