Cod sursa(job #3330070)

Utilizator Barzoi-AlbertoBarzoi Alberto Florian Barzoi-Alberto Data 17 decembrie 2025 13:06:25
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
using namespace std;
ifstream cin ("cautbin.in");
ofstream cout ("cautbin.out");
int n, m, a[100000], x, poz, dr, st, mij, nr;

int main()
{
	cin>>n;
	for (int i=1; i<=n; i++)
		cin>>a[i];
	cin>>m;
	for (int i=1; i<=m; i++)
	cin>>nr>>x;

	st=1;
	dr=n;
	poz=0;
	while (st<=dr)
	{	mij=(dr+st)/2;
		if (a[mij]==x) {
			poz=mij;
			st=mij+1;
		}
		else if (a[mij]>x) dr=mij-1;
		else st=mij+1;
	}
	if (poz==0)cout<<-1<<'\n';
	else cout<<poz<<'\n';

	st=1;
	dr=n;
	poz=0;
	while (st<=dr)
	{	mij=(dr+st)/2;
		if (a[mij]<=x) {
			poz=mij;
			st=mij+1;
		}
		else dr=mij-+1;
	}
	cout<<poz<<'\n';

	st=1;
	dr=n;
	poz=0;
	while (st<=dr)
	{	mij=(dr+st)/2;
		if (a[mij]>=x) {
			poz=mij;
			dr=mij-1;
		}
		else st=mij+1;
	}
	cout<<poz;

	return 0;
}