Pagini recente » Cod sursa (job #851108) | Cod sursa (job #2310692) | Cod sursa (job #2452577) | Clasament live1 | Cod sursa (job #649978)
Cod sursa(job #649978)
#include<fstream>
#define maxn 100001
using namespace std;
int n, v[maxn], m, x, i, j, choice;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int cautare_binara(int v[], int value, int low, int high)
{while(low<=high)
{int mid=low+(high-low)/2;
if(v[mid]==value) return mid;
if(value<v[mid])
high=mid-1;
if(value>v[mid])
low=mid+1;
}
return -1;
}
int c0(int x)
{int poz=cautare_binara(v, x, 1, n);
if(poz==-1) return -1;
for(i=poz; i<n && v[i+1]==x; i++)
;
return i;
}
int c1(int x)
{int poz=cautare_binara(v, x, 1, n);
for(i=poz; i<n && v[i+1]<=x; i++)
;
return i;
}
int c2(int x)
{int poz=cautare_binara(v, x, 1, n);
for(i=poz; i>0 && v[i-1]>=x; i--)
;
return i;
}
int main()
{f>>n;
for(j=1; j<=n; j++)
f>>v[j];
f>>m;
for(j=1; j<=m; j++)
{f>>choice;
f>>x;
switch(choice)
{case 0: g<<c0(x)<<endl;
break;
case 1: g<<c1(x)<<endl;
break;
case 2: g<<c2(x)<<endl;
break;
}
}
f.close();
g.close();
return 0;
}