Pagini recente » Cod sursa (job #1824818) | Cod sursa (job #1309879) | Cod sursa (job #1775602) | Cod sursa (job #2516627) | Cod sursa (job #1936685)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n,m,a[100005],p,x,last;
int st,dr,med,ok=0,rez,mij,c;
int main()
{
cin>>n;
for(int i=1;i<=1;i++)
cin>>a[i];
sort(a+1,a+n+1);
cin>>x;
st=1;
dr=n;
while(st<dr)
{
med=(st+dr)/2;
if(x==a[med])
{
rez=a[med];
ok=1;
}
if(x>a[med])
{
st=med+1;//cout<<med<<" ";
}
if(x<a[med])
{
dr=med-1;//cout<<med<<" ";
}
}
while(st<=dr) {
mij=(st+dr)/2;
if(x==a[mij]) {
if (c==0) {
last=mij;
st=mij+1;
}
if (c==1) {
last=mij;
st=mij+1;
}
if (c==2) {
last=mij;
dr=mij-1;
}
}
if(x<a[mij]) {
dr=mij-1;
if (c==2) {
last=mij;
}
}
if(x>a[mij]) {
st=mij+1;
if (c==1) {
last=mij;
}
}
}
if(ok=1)
cout<<rez;
else
cout<<-1;
cout<<last;
return 0;
}