Untitled

 avatar
unknown
plain_text
10 months ago
1.5 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;
bool compare_pair(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first != b.first)
        return a.first < b.first; // Sort by first value in ascending order
    else
        return a.second > b.second; // If first values are equal, sort by second value in descending order
}
void find_and_print_range(const vector<pair<int, int>>& pairs, int range_start, int range_end) {
    auto low_it = lower_bound(pairs.begin(), pairs.end(), make_pair(range_start, INT_MIN), 
        [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.first < b.first;
        });

    // Using STL upper_bound to find the ending position, but making sure to include range_end if it's equal
    auto high_it = upper_bound(pairs.begin(), pairs.end(), make_pair(range_end, INT_MAX), 
        [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.first < b.first;
        });


    int count = distance(low_it, high_it);

    cout << count << " mobiles are available" << endl;

    for (auto it = low_it; it != high_it; ++it) {
        cout << "Price " << it->first << " Speed " << it->second << "\n";
    }
}


int main() {
    int n; 
    cin >> n;
    
    vector<pair<int,int>>a(n);
    
    for(int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    sort(begin(a), end(a), compare_pair);
    int q, l, r;
    cin >> q;
    for(int i = 0; i < q; i++) {
        cin >> l >> r;
        find_and_print_range(a, l, r);
    }
    
}
Editor is loading...
Leave a Comment