Cod sursa(job #1690880)

Utilizator AndreiTudorSpiruAndrei Spiru AndreiTudorSpiru Data 16 aprilie 2016 08:22:00
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,i,v[1001],x,y,t,m,k;
int main()
{f>>n;
for(i=1;i<=n;i++)
    f>>v[i];
sort(v+1,v+n+1);
f>>m;
for(k=1;k<=m;k++)
{f>>t>>y;
if(t==0)
{x=upper_bound(v+1,v+n+1,y)-v-1;
if(x>=1&&x<=n&&v[x]==y)g<<x<<'\n';
else g<<"-1"<<'\n';
}
  else if(t==1)
  {x=lower_bound(v+1,v+n+1,y+1)-v-1;
  g<<x<<'\n';
      }
else{
    x=upper_bound(v+1,v+n+1,y-1)-v;
    g<<x<<'\n';
}
}

    return 0;
}