Advertisement
LZsolar

SMMR-085: Relax

Apr 8th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. vector<pair<int,int>> co;
  5. map<int,int> visited;
  6.  
  7. int find(int i){
  8.     int f=0,l=n-1;
  9.     while(f<=l){
  10.         int m=(f+l)/2;
  11.         if(co[m].first==i){
  12.             while(co[m].first==i&&m>=0){m--;}
  13.            
  14.             return co[m+1].second;
  15.         }
  16.        
  17.         if(co[m].first>i){l=m-1;}
  18.         else{f=m+1;}
  19.     }
  20.    
  21.     return -1;
  22. }
  23.  
  24. int main(){
  25.     scanf("%d",&n);
  26.     for(int i=1;i<=n;i++){
  27.         int a; scanf("%d",&a);
  28.        
  29.         co.push_back({a,i});
  30.     }
  31.     sort(co.begin(),co.end());
  32.     int q; scanf("%d",&q);
  33.     for(int i=0;i<q;i++){
  34.         int a,ans;
  35.         scanf("%d",&a);
  36.         if(visited[a]!=0){ans=visited[a];}
  37.         else{ans=find(a); visited[a]=ans;}
  38.         if(ans!=-1){
  39.             printf("%d\n",ans);
  40.         }
  41.         else{printf("Not found\n");}
  42.     }
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement