Cod sursa(job #2392241)
Utilizator | Data | 29 martie 2019 20:13:18 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.73 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,a[100001],m,c,nr,st,dr,poz,x;
int main()
{
f>>n;
for(int i=1; i<=n; i++)
f>>a[i];
sort(a+1,a+n+1);
f>>m;
for(int i=1; i<=m; i++)
{
f>>c>>nr;
if(!c)
{
st=1;
dr=m;
poz=0;
while(st<=dr&&poz==0)
{
x=(st+dr)/2;
if(a[m]==nr)
poz=x;
if(a[m]<nr)
st=x+1;
else
dr=x;
}
if(a[poz]==a[poz+1])
while(a[poz]==a[poz+1])
poz++;
g<<poz<<'\n';
}
else if(c==1)
{
st=1;
dr=m;
poz=0;
while(st<=dr&&poz==0)
{
x=(st+dr)/2;
if(a[m]==nr)
poz=m;
if(a[x]<x)
st=x+1;
else
dr=x;
}
if(poz!=0)
g<<poz<<'\n';
else
g<<st<<'\n';
}
else
{
st=1;
dr=m;
poz=0;
while(st<=dr&&poz==0)
{
x=(st+dr)/2;
if(a[m]==nr)
poz=x;
if(a[m]<nr)
st=x+1;
else
dr=x;
}
if(a[poz]==a[poz+1])
while(a[poz]==a[poz+1])
poz++;
g<<poz+1<<'\n';
}
}
return 0;
}