Cod sursa(job #1606581)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 20 februarie 2016 13:12:44
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

#define MAXN 100050

int x[MAXN];
int N, M;

int main()
{
	fin >>N;
	for (int i = 1; i <= N; ++i)
		fin >>x[i];

	sort (x+1, x+N+1);
	fin >>M;
	for (int i = 1; i <= M; ++i)
	{
		int t, y;
        fin >>t >>y;
        switch (t)
        {
		case 0:
			{int pos = upper_bound(x+1, x+1+N, y) - x - 1;
			if (pos >= 1 && pos <= N && x[pos] == y)
				fout <<pos <<'\n';
			else
				fout <<-1 <<'\n';
			break;}
		case 1:{
			int pos = lower_bound(x+1, x+1+N, y+1) - x - 1;
			fout <<pos <<'\n';
			break;}
		case 2:{
			int pos = upper_bound(x+1, x+1+N, y-1) - x;
			fout <<pos <<'\n';
			break;}
        }
	}

    return 0;
}