Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int floor(vector<int>& arr,int k,int n){
- // functions searches an index i such that arr[i] <= k
- // it is called floor
- if(k < arr[n - 1]) return n; // edge case handled
- // edge case explanation:
- // ex:
- // indices: 0 1 2 3 4
- // array: 100 50 40 20 10
- // the index that suits for 5 in above array at index 5 i.e., the size of array.
- int l=0,hi=n-1;
- int ans = 0;
- while(l<=hi){
- int mid=(l+hi)/2;
- if(arr[mid]>k){
- l=mid+1;
- }
- else{
- ans = mid;
- hi=mid-1;
- }
- }
- return ans; // returns the index where the book is suitable
- }
- void removeDuplicates(vector<int>& arr) {
- int n = arr.size();
- int i = 0, j = 0;
- while(j < n) {
- int cur = arr[j];
- while(j < n && arr[j] == cur) {
- j++;
- }
- arr[i] = cur;
- i++;
- }
- int toRemove = n - i;
- while(toRemove-- > 0) {
- arr.pop_back();
- }
- }
- int main() {
- int t;
- cin>>t;
- while(t--){
- int n;
- cin>>n;
- vector<int> arr1(n);
- for(int i=0;i<n;i++){
- cin>>arr1[i];
- }
- removeDuplicates(arr1); // function removes the duplicates from the reverse sorted array
- n = arr1.size(); // new size of the array
- int m;
- cin>>m;
- vector<int> arr2(m);
- for(int i=0;i<m;i++){
- cin>>arr2[i];
- }
- int f;
- for(int i=0;i<m;i++){
- // int f=ceil(arr1,arr2[i],n);
- int f = floor(arr1, arr2[i], n); // changed from ceil to floor
- int rank = f + 1;
- cout<< rank << " ";
- }
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment