Pagini recente » Cod sursa (job #499252) | Cod sursa (job #2042551) | Cod sursa (job #391626) | Cod sursa (job #55168) | Cod sursa (job #903330)
Cod sursa(job #903330)
#include <iostream>
#include <fstream>
using namespace std;
int n,m,i,j,k,r,b,c,a[100000];
int caut1(int x,int c,int d)
{
int m;
if (c<=d)
{
m=(d+c)/2;
if (a[m]==x&&a[m+1]!=x) return m;
else
if (a[m]>x) caut1(x,c,m-1);
else caut1(x,m+1,d);
if ((a[m]<x&&a[m+1]>x )|| (a[m-1]<x&&a[m]>x)) return -1;
}
}
int caut2(int x,int c,int d)
{
int m;
if (c<=d)
{
m=(c+d)/2;
if ((a[m]==x&&a[m-1]<x)||(a[m]<x&&a[m+1]>x)) return m;
else
if (a[m]>x) caut2(x,c,m-1);
else caut2(x,m+1,d);
}
}
int caut3 (int x,int c,int d)
{
int m;
if (c<=d)
{
m=(c+d)/2;
if ((a[m]==x&&a[m+1]>x)||(a[m]>x&&a[m-1]<x)) return m;
else
if (a[m]>x) caut3(x,c,m-1);
else caut3(x,m+1,d);
}
}
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
f>>m;
while(i<=m)
{
f>>r>>b;c=1;
if (r==0) g<<caut1(b,c,n)<<"\n";
else
if (r==1) g<<caut2(b,c,n)<<"\n";
else g<<caut3(b,c,n)<<"\n";
}
return 0;
}