Concert Tickets

There are n concert tickets available, each with a certain price. Then, customers arrive, one after another.

Each customer announces the maximum price he or she is willing to pay for a ticket, and after this, they will get a ticket with the nearest possible price such that it does not exceed the maximum price.

Example:

Input: n = 5, m = 3 , arr = {5, 3, 7, 8, 5},brr = {4, 8, 2}

Output:

3 8 -1

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void concertTickets(int nint mvector<int&arr,
                    vector<int&brr)
{

    multiset<intms;
    for (int i = 0i < ni++)
    {

        ms.insert(arr[i]);
    }
    for (int i = 0i < mi++)
    {

        auto it = ms.upper_bound(brr[i]);
        if (it == ms.begin())
            cout << -1 << "\n";
        else
        {
            it = prev(it);
            cout << *it << "\n";
            ms.erase(it);
        }
    }
}

int main()
{
    int n = 5m = 3;
    vector<intarr = {53785};
    vector<intbrr = {482};

    concertTickets(nmarrbrr);

    return 0;
}


No comments:

Post a Comment