Cod sursa(job #2480415)
Utilizator | Data | 25 octombrie 2019 16:30:18 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 2.47 kb |
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, a, i, st, dr, mij, l, v[1000001], m, b, nr;
bool ok;
int main()
{
fin>>n;
for(i=1; i<=n; i++)
fin>>v[i];
fin>>m;
for(i=1; i<=m; i++)
{
fin>>a>>b;
if(a==0)
{
st=1;
dr=n;
ok=false;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==b)
{
l=mij;
ok=true;
break;
}
else
{
if(v[mij]==b)
dr=mij-1;
else
st=mij+1;
}
}
if(ok==true)
{
while(v[l]==v[l+1])
l++;
fout<<l<<endl;
}
else
fout<<-1<<endl;
}
if(a==1)
{
st=1;
dr=n;
ok=false;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==b)
{
l=mij;
ok=true;
break;
}
else
{
if(v[mij]==b)
dr=mij-1;
else
st=mij+1;
}
}
if(ok==true)
{
while(v[l]==v[l+1])
l++;
fout<<l<<endl;
}
else
fout<<-1;
}
if(a==2)
{
st=1;
dr=n;
ok=false;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==b)
{
l=mij;
ok=true;
break;
}
else
{
if(v[mij]==b)
dr=mij-1;
else
st=mij+1;
}
}
if(ok==true)
{
while(v[l]==v[l-1])
l--;
fout<<l;
}
else
fout<<-1;
}
}
return 0;
}