Cod sursa(job #1769401)

Utilizator Grama911Grama Andrei Grama911 Data 2 octombrie 2016 15:04:18
Problema Cautare binara Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,m,a[100000];

int main()
{
	f >> n;
	for (int i = 1; i <= n; i++)
		f >> a[i];
	f >> m;
	for (int i = 0; i < m; i++)
	{
		int q, x;
		f >> q >> x;
		if (q == 0)
		{
			int max = -1000000000;
			int i = 1, ok = 0;
			while (a[i] <= x)
			{
				if (a[i] == x&&a[i] < a[i + 1])
				{
					max = i;
					ok = 1;
				}
				i++;
			}
			if (ok == 1)
				g << max << "\n";
			else
				g << -1 << "\n";
		}
		if (q == 1)
		{
			int i = 1;
			while (a[i] <= x)
				i++;
			g << i-1 << "\n";
		}
		if (q == 2)
		{
			int i = 1;
			while (a[i] <= x)
			{
				i++;
				if (a[i] == a[i + 1])
					break;
			}
			if (a[i-1] == x)
				g << i-1 << "\n";
			else
				g << i << "\n";
		}
	}
	//cin.get();
	return 0;
}