Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.004 s | 1 KBi |
#2 |
Correcto
|
0.007 s | 1 KBi |
#3 |
Correcto
|
0.006 s | 9 KBi |
#4 |
Correcto
|
0.006 s | 1 KBi |
#5 |
Correcto
|
0.006 s | 42 KBi |
#6 |
Correcto
|
0.007 s | 2 KBi |
#7 |
Correcto
|
0.006 s | 27 KBi |
#8 |
Correcto
|
0.009 s | 1 KBi |
#9 |
Correcto
|
0.009 s | 1 KBi |
#10 |
Correcto
|
0.009 s | 2 KBi |
#include<iostream> #include<algorithm> #include<vector> using namespace std; int bSearch(vector<int>& vals, int target){ int l = 0; int r = vals.size()-1; int res = -1; while(l<=r){ int m = (l+r)/2; if(vals[m]<target){ l = m+1; } else if(vals[m]==target){ res = m; l = m+1; } else{ r = m-1; res = m; } } return res; } int main(){ int n, c; cin>>n; vector<int>vals(n); for(auto &val: vals){ cin>>val; } sort(vals.begin(),vals.end()); cin>>c; for(int i=0;i<c;i++){ int target; cin>>target; int indexElement = bSearch(vals,target); if(indexElement!=-1){ if(vals[indexElement]!=target){ cout<<vals.size()-indexElement<<endl; } else{ cout<<vals.size()-1-indexElement<<endl; } } else{ if(target>vals[vals.size()-1]){ cout<<0<<endl; } else{ cout<<vals.size()<<endl; } } } return 0; }