1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
#include <fstream>
using namespace std;
int v[100001], N, M, mij, x, nr;
int cautare0(int k, int inf, int sup)
{
int p=-1;
while (inf<=sup)
{mij=inf+(sup-inf)/2;
if (v[mij]==k)
{p=mij;
inf=mij+1;}
else
if (v[mij]<k)
inf=mij+1;
else
sup=mij-1;}
return p;
}
int cautare1 (int k, int inf, int sup)
{
int p=0, q;
while (inf<=sup)
{mij=inf+(sup-inf)/2;
if (v[mij]<=k)
{q=k-v[mij];
p=mij;
if (k-v[mij+1]>q)
sup=mij-1;}
else
inf=mij+1;}
return p;
}
int cautare2 (int k, int inf, int sup)
{
int p=0, q;
while (inf<=sup)
{mij=inf+(sup-inf)/2;
if (v[mij]>=k)
{q=v[mij]-k;
p=mij;
if (v[mij+1]-k>=q)
sup=mij-1;}
else
inf=mij+1;}
return p;
}
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>N;
for (int i=1; i<=N; i++)
f>>v[i];
f>>M;
for (int j=0; j<M; j++)
{f>>nr>>x;
if (nr==0)
g<<cautare0(x,1,N);
if (nr==1)
g<<cautare1(x,1,N);
if (nr==2)
g<<cautare2(x,1,N);}
f.close();
g.close();
return 0;
}
|