Cod sursa(job #3229563)
Utilizator | Data | 16 mai 2024 17:37:14 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 6.55 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100001][3];
int main() {
int n, m;
f>>n;
int index=0;
int x;
f>>x;
v[index][0]=x;
v[index][1]=0;
int cnr=x;
for(int i=1; i<n; i++) {
int x;
f>>x;
if(x!=cnr) {
cnr=x;
v[index][2]=i-1;
index++;
v[index][0]=x;
v[index][1]=i;
}
//v[i]=x;
}
v[index][2]=n-1;
n=index+1;
f>>m;
for(int i=1; i<=m; i++) {
int cerinta;
f>>cerinta;
/*
if(cerinta==0) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
while(stg<dr && !gasit) {
int mij=(stg+dr)/2;
if(v[mij]>x && v[mij-1]<x) {
gasit=0;
//mij++;
break;
}
if(v[mij]<x && v[mij+1]>x) {
gasit=0;
// mij--;
break;
}
if(v[mij]==x)
if(v[mij+1]!=x) {
g<<mij+1<<'\n';
gasit=1;
} else {
stg=mij;
}
if(v[mij]!=x)
if(v[mij-1]==x) {
g<<mij+1<<'\n';
gasit=1;
} else {
if(v[mij]>x)
dr=mij;
else
stg=mij;
}
}
if(!gasit)
g<<-1<<'\n';
}
if(cerinta==1) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
int mij;
while(stg<dr && !gasit && x<v[n-1]) {
mij=(stg+dr)/2;
if(v[mij]>x && v[mij-1]<x) {
gasit=1;
mij++;
break;
}
if(v[mij]<x && v[mij+1]>x) {
gasit=1;
mij--;
break;
}
if(v[mij]==x)
if(v[mij+1]!=x) {
g<<mij+1<<'\n';
gasit=1;
} else {
stg=mij;
}
if(v[mij]!=x)
if(v[mij-1]==x) {
g<<mij+1<<'\n';
gasit=1;
} else {
if(v[mij]>x)
dr=mij;
else
stg=mij;
}
}
if(!gasit)
if(x>v[n-1])
g<<n-1<<'\n';
else
g<<mij+1<<'\n';
}
if(cerinta==2) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
int mij;
while(stg<dr && !gasit && x>v[0]) {
mij=(stg+dr)/2;
if(v[mij]<x && v[mij+1]>x) {
gasit=1;
mij++;
break;
}
if(v[mij]<x && v[mij+1]>x) {
gasit=1;
mij--;
break;
}
//cout<<stg<<" "<<dr<<" "<<m;
if(v[mij]==x)
if(v[mij-1]!=x) {
g<<mij+1<<'\n';
gasit=1;
} else {
dr=mij;
}
if(v[mij]!=x)
if(v[mij+1]==x) {
g<<mij+1<<'\n';
gasit=1;
} else {
if(v[mij]>x)
dr=mij;
else
stg=mij;
}
}
if(!gasit)
if(x<v[0])
g<<1<<'\n';
else
g<<mij+1<<'\n';
}
*/
if(cerinta==0) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
int mij;
while(stg<=dr && !gasit) {
mij=(stg+dr)/2;
if(v[mij][0]==x){
g<<v[mij][2]+1<<endl;
gasit=1;
}
if(v[mij][0]>x)
dr=mij-1;
else
stg=mij+1;
}
if(!gasit)
g<<-1<<endl;
}
if(cerinta==1) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
int mij;
while(stg<=dr && !gasit) {
mij=(stg+dr)/2;
if(v[mij][0]==x){
g<<v[mij][2]+1<<endl;
gasit=1;
}
if(v[mij][0]>x&& v[mij-1][0]<x){
g<<v[mij-1][2]+1<<endl;
gasit=1;
}
if(v[mij][0]<x&& v[mij+1][0]>x){
g<<v[mij][2]+1<<endl;
gasit=1;
}
if(v[mij][0]>x)
dr=mij-1;
else
stg=mij+1;
}
if(!gasit)
g<<v[n-1][2]+1<<endl;
}
if(cerinta==2) {
int x;
f>>x;
bool gasit=0;
int stg=0, dr=n-1;
int mij;
while(stg<=dr && !gasit) {
mij=(stg+dr)/2;
if(v[mij][0]==x){
g<<v[mij][1]+1<<endl;
gasit=1;
}
if(v[mij][0]>x&& v[mij-1][0]<x){
g<<v[mij][1]+1<<endl;
gasit=1;
}
if(v[mij][0]<x&& v[mij+1][0]>x){
g<<v[mij+1][1]+1<<endl;
gasit=1;
}
if(v[mij][0]>x)
dr=mij-1;
else
stg=mij+1;
}
if(!gasit)
g<<v[0][2]+1<<endl;
}
}
//4380 4388
return 0;
}