There are n concert tickets available, each with a certain price. Then, m 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 n, int m, vector<int> &arr,vector<int> &brr){multiset<int> ms;for (int i = 0; i < n; i++){ms.insert(arr[i]);}for (int i = 0; i < m; i++){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 = 5, m = 3;vector<int> arr = {5, 3, 7, 8, 5};vector<int> brr = {4, 8, 2};concertTickets(n, m, arr, brr);return 0;}
No comments:
Post a Comment