Cod sursa(job #936362)
Utilizator | Data | 6 aprilie 2013 20:15:32 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.74 kb |
#include <fstream>
#include<stdio.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n, v[100003], m, pmax, k, i, u, p, mij, x;
int main() {
f>>n;
for(i=1; i<=n; i++)
f>>v[i];
f>>m;
for(;m--;){
f>>k>>x;
if(k==0){
pmax=-1;
p=1; u=n;
while(p<=u){
mij=p+(u-p)/2;
if(v[mij]==x){
if(mij>pmax)
pmax=mij;
p=mij+1;
}
else{
if(v[mij]<x)
p=mij+1;
else{
if(v[mij]>x)
u=mij-1;
}
}
}
g<<pmax<<"\n";
}
if(k==1){
pmax=-1;
p=1; u=n;
while(p<=u){
mij=p+(u-p)/2;
if(v[mij]<=x){
if(mij>pmax)
pmax=mij;
p=mij+1;
}
else{
if(v[mij]>x){
u=mij-1;
}
}
}
g<<pmax<<"\n";
}
if(k==2){
pmax=n;
p=1; u=n;
while(p<=u){
mij=p+(u-p)/2;
if(v[mij]>=x){
if(mij<pmax)
pmax=mij;
u=mij-1;
}
else{
if(v[mij]<x)
p=mij+1;
}
}
g<<pmax<<"\n";
}
}
return 0;
}