Cod sursa(job #398601)

Utilizator Mishu91Andrei Misarca Mishu91 Data 19 februarie 2010 00:03:38
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <algorithm>

using namespace std;

const int MAX_N = 100005;

ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");

int N, M, V[MAX_N];

int main()
{
	fin >> N;
	for(int i = 1; i <= N; ++i)
		fin >> V[i];
	
	fin >> M;
	for(int i = 1; i <= M; ++i)
	{
		int t, x, p;
		fin >> t >> x;
		if(t == 0)
			if(V[p = upper_bound(V+1, V+N+1, x) - V - 1] == x)
				fout << p << "\n";
			else
				fout << "-1\n";
		else if(t == 1)
			fout << upper_bound(V+1, V+N+1, x) - V - 1 << "\n";
		else
			fout << lower_bound(V+1, V+N+1, x) - V << "\n";
	}
	
}