Advertisement
leanchec

ultimo menor ou igaul a k

Jun 5th, 2025
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> v;
  5. int k;
  6.  
  7. int valor(int x){
  8.     if(v[x]>k){
  9.         return 1;
  10.     }
  11.     else{
  12.         return 0;
  13.     }
  14. }
  15.  
  16. int main(){
  17.     int n, q;
  18.     cin >> n >> q;
  19.  
  20.     for(int i=0; i<n; i++){
  21.         int val;
  22.         cin >> val;
  23.         v.push_back(val);
  24.     }
  25.  
  26.     for(int i=0; i<q; i++){
  27.         cin >> k;
  28.  
  29.         int L=0;
  30.         int R=n-1;
  31.  
  32.         if(valor(n-1)==0){
  33.             cout << n << '\n';
  34.             continue;
  35.         }
  36.  
  37.         while(L<R){
  38.             int mid=(L+R)/2;
  39.  
  40.             if(valor(mid)==1){
  41.                 R=mid;
  42.             }
  43.             else{
  44.                 L=mid+1;
  45.             }
  46.         }
  47.  
  48.         //primeiro maior que k = L+1
  49.         //ultimo <=k = L
  50.  
  51.         cout << L << '\n';
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement