Cod sursa(job #1611811)
Utilizator | Data | 24 februarie 2016 14:36:43 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.97 kb |
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
long a[100001];
int i,n,m,cod,x,st,dr,mij,gasit=0,k,j;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
f>>m;
for(j=1;j<=m;j++){
f>>cod;
f>>x;
if(cod==0){
st=1;
dr=n;
while(gasit==0 && st<=dr){
mij=(st+dr)/2;
if(a[mij]==x)
gasit=1;
else
if(a[mij]>x)
dr=mij-1;
else
st=mij+1;
}
k=mij+1;//inseamna ca nu s-a iesit din vector
while(a[k]==x && k<=n)
k++;
g<<k-1<<endl;
}
else
if(cod==1){
st=1;
dr=n;
while(gasit==0 && st<=dr){
mij=(st+dr)/2;
if(a[mij]<=x)
gasit=1;
else
if(a[mij]>x)
dr=mij-1;
else
st=mij+1;
}
k=mij+1;
while(a[k]<=x && k<=n)
k++;
g<<k-1<<endl;
}
else
if(cod==2){
st=1;
dr=n;
while(gasit==0 && st<=dr){
mij=(st+dr)/2;
if(a[mij]>=x)
gasit=1;
else
if(a[mij]>x)
dr=mij-1;
else
st=mij+1;
}
k=mij+1;
while(a[k]>=x && k<=n)
k--;
g<<k+1<<endl;
}
}
return 0;
}