Cod sursa(job #2287402)
Utilizator | Data | 21 noiembrie 2018 20:44:22 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.41 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream fut("cautbin.out");
int v[100005],dr,st,mid,n,m,x,k,cx;
int main()
{
f>>n;
for(int i = 1; i <= n; i++)
f>>v[i];
f>>x;
dr=n;
st=1;
while(st<dr)
{
mid=(st+dr+1)/2;
if(v[mid]>x)
dr=mid-1;
else
st=mid;
}
if(v[dr]==x)
k=dr;
else
k=-1;
if(k>0)
fut<<k<<'\n'<<k<<'\n';
else
{cx=x-1;
st=1;
dr=n;
while(st<dr)
{
mid=(st+dr+1)/2;
if(v[mid]>cx)
dr=mid-1;
else st=mid;
}
fut<<dr<<'\n';
}
if(k>0)
{
int ok=1;
st=1;
while(ok)
{
if(v[st]==x)
ok=0;
else
st++;
}
if(ok==0)
fut<<st;
}
else
{
x=x+1;
int ok=1;
st=1;
while(ok)
{
if(v[st]==x)
ok=0;
else
st++;
}
if(ok==0)
fut<<dr<<'\n'<<st;
}
return 0;
}