Cod sursa(job #2260791)
Utilizator | Data | 15 octombrie 2018 16:22:08 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.63 kb |
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100001];
int main() {
int n,m,in,sf,c,x,poz,mij;
f>>n;
for (int i=1; i<=n; i++) {
f>>v[i];
}
f>>m;
for (int i=1; i<=m; i++) {
poz=0;
in=1;
sf=n;
f>>c>>x;
while (in<=sf &&poz==0) {
mij=(in+sf)/2;
if (v[mij]==x) {
poz=mij;
} else {
if (v[mij]>x) {
sf=mij-1;
} else {
in=mij+1;
}
}
}
if (c==0) {
while (v[poz+1]==x) {
poz++;
}
if (poz!=0) {
g<<poz<<'\n';
} else {
g<<-1<<'\n';
}
}
if (poz==0){
poz=in;
}
if (c==1) {
if (v[poz]==x)
{
while (v[poz+1]==x) {
poz++;
}
g<<poz<<'\n';
}else
{
g<<poz-1<<'\n';
}
}
if (c==2) {
if (v[poz]==x)
{
while (v[poz-1]==x)
{
poz--;
}
g<<poz<<'\n';
}else
{
g<<poz<<'\n';
}
}
}
return 0;
}