Mai intai trebuie sa te autentifici.
Cod sursa(job #668681)
Utilizator | Data | 25 ianuarie 2012 14:17:50 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.78 kb |
// http://infoarena.ro/problema/cautbin
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXSIZE = 100001;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
int length,questions;
vector<int> v;
int main()
{
in >> length;
for(int i=1;i<=length;i++)
{
in >> questions; // tmp variable
v.push_back(questions);
}
in >> questions;
int type,value;
vector<int>::iterator it;
for(int i=1;i<=questions;i++)
{
in >> type >> value;
switch(type)
{
case 0:
{
it = upper_bound(v.begin(),v.end(),value);
if(it == v.end())
{
if(*v.rbegin() == value)
out << v.size() << "\n";
else
out << "-1\n";
}
else
{
if(it != v.begin())
--it;
if(*it == value)
out << (it - v.begin()) + 1 << "\n";
else
out << "-1\n";
}
} break;
case 1:
{
it = upper_bound(v.begin(),v.end(),value);
//if(it != v.begin());
// --it;
out << (it - v.begin()) << "\n";
} break;
case 2:
{
it = lower_bound(v.begin(),v.end(),value);
out << (it - v.begin()) + 1 << "\n";
} break;
}
}
return (0);
}