Cod sursa(job #801395)

Utilizator toranagahVlad Badelita toranagah Data 24 octombrie 2012 10:32:40
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

int sir[100100];
int N, M;

int main(int argc, char const *argv[])
{
	fin >> N;
	for (int i = 0; i < N; ++i) {
		fin >> sir[i];
	}
	fin >> M;
	for (int i = 0; i < M; ++i) {
		int c, x, pos;
		fin >> c >> x;
		if (c == 0) {
			pos = upper_bound(sir, sir+N, x);
			if (pos == sir+N) {
				fout << -1;
			} else {
				fout << pos + 1;
			}
			fout << '\n';
		} else if (c == 1) {
			pos = lower_bound(sir, sir+N, x);
			fout << pos + 1 << '\n';
		} else if (c == 2) {
			pos = upper_bound(sir, sir+N, x);
			fout << pos + 1 << '\n';
		} else {
			fout << "ERROR" << endl;
		}
	}
	return 0;
}