Cod sursa(job #2343544)
Utilizator | Data | 14 februarie 2019 08:49:31 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 3.42 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[100001],st,dr,ok,m,i,n,x,b[100001],h,c,y;
void read()
{
f>>n;
for(i=1; i<=n; i++)
f>>a[i];
}
void sortare()
{
int ok=0,aux;
while(ok==0)
{
ok=1;
for(i=1; i<n; i++)
if(a[i]>a[i+1])
{
aux=a[i];
a[i]=a[i+1];
a[i+1]=aux;
ok=0;
}
}
}
int main()
{
read();
sortare();
f>>h;
while(h)
{
h--;
f>>c>>x;
if(c==0)
{
st=1;
dr=n;
ok=0;
while(st<=dr&&ok==0)
{
m=(st+dr)/2;
if(a[m]==x)
{
ok=1;
y=m;
}
else
{
if(x>a[m])
st=m+1;
else
dr=m-1;
}
}
if(ok==0)
g<<-1<<'\n';
else
{
i=y;
while(a[i]==x)
{
i++;
}
g<<i-1<<'\n';
}
}
if(c==1)
{
st=1;
dr=n;
ok=0;
while(st<=dr&&ok==0)
{
m=(st+dr)/2;
if(a[m]==x)
{
ok=1;
y=m;
}
else
{
if(x>a[m])
st=m+1;
else
dr=m-1;
}
}
if(ok==0)
g<<-1<<'\n';
else
{
i=y;
while(a[i]==x)
{
i++;
}
g<<i-1<<'\n';
}
}
if(c==2)
{
st=1;
dr=n;
ok=0;
while(st<=dr&&ok==0)
{
m=(st+dr)/2;
if(a[m]==x)
{
ok=1;
y=m;
}
else
{
if(x>a[m])
st=m+1;
else
dr=m-1;
}
}
if(ok==0)
g<<-1<<'\n';
else
{
i=y;
while(a[i]==x)
{
i--;
}
g<<i+1<<'\n';
}
}
}
/*
for(j=1; j<=h; j++)
{
st=1;
dr=n;
ok=0;
while(st<=dr&&ok==0)
{
m=(st+dr)/2;
if(a[m]==b[j])
{
ok=1;
cout<<1<<" ";
}
else
{
if(a[m]<b[j])
st=m+1;
else
dr=m-1;
}
}
if(ok==0)
cout<<0<<" ";
}
*/
return 0;
}