Pagini recente » Cod sursa (job #1726602) | Cod sursa (job #1170796) | Cod sursa (job #1049200) | Cod sursa (job #3126070) | Cod sursa (job #2500629)
#include <fstream>
using namespace std;
int cautareBinara0(int x, int a[], int n)
{
int lo=0, hi=n, mid, pos=-1;
while(lo<=hi)
{
mid=(lo+hi)/2;
if(a[mid]==x)
{
pos=mid;
while(a[pos]==x)
pos++;
return pos-1;
}
else if(a[mid]<x)
lo=mid+1;
else
hi=mid-1;
}
return -1;
}
int cautareBinara2(int x, int a[], int n)
{
int lo=0, hi=n, mid, pos=-1;
while(lo<=hi)
{
mid=(lo+hi)/2;
if(a[mid]==x)
{
pos=mid;
while(a[pos]==x)
pos--;
return pos+1;
}
else if(a[mid]<x)
lo=mid+1;
else
hi=mid-1;
}
return -1;
}
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int main()
{
int n, m, arr[100000]; fin>>n;
for(int pas=0; pas<n; pas++)
fin>>arr[pas];
fin>>m;
int x; char op;
for(int i=0; i<m; i++)
{
fin>>op>>x;
switch(op)
{
case 2:
{
fout<<cautareBinara2(x, arr, n)+1<<endl;
break;
}
default:
{
fout<<cautareBinara0(x, arr, n)+1<<endl;
break;
}
}
}
fin.close();
fout.close();
return 0;
}