Cod sursa(job #1618521)
Utilizator | Data | 27 februarie 2016 21:03:11 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | c | Status | done |
Runda | Arhiva educationala | Marime | 1.64 kb |
#include <stdio.h>
FILE *in,*out;
int n,v[100001],m,i,x,st,dr,q,p;
int main()
{
in=fopen("cautbin.in","r");
out=fopen("cautbin.out","w");
fscanf(in,"%d",&n);
for(i=1; i<=n; i++)
fscanf(in,"%d",&v[i]);
fscanf(in,"%d",&p);
for(i=1; i<=p; i++)
{
fscanf(in,"%d%d",&q,&x);
if(q==0)
{
st=0;
dr=n;
while(st<dr)
{
m=(st+dr+1)/2;
if(x>=v[m])
{
st=m;
}
else
{
dr=m-1;
}
}
if(x==v[st])
{
fprintf(out,"%d\n",st);
}
else
{
fprintf(out,"-1\n");
}
}
if(q==1)
{
st=0;
dr=n;
while(st<dr)
{
m=(st+dr+1)/2;
if(x>=v[m])
{
st=m;
}
else
{
dr=m-1;
}
}
fprintf(out,"%d\n",st);
}
if(q==2)
{
st=0;
dr=n;
while(st<dr)
{
m=(st+dr)/2;
if(x<=v[m])
{
dr=m;
}
else
{
st=m+1;
}
}
fprintf(out,"%d\n",st);
}
}
return 0;
}