Find the maximum cost of an array of pairs choosing at most K pairs
Given an array of pairs arr[], the task is to find the maximum cost choosing at most K pairs. The cost of an array of pairs is defined as product of the sum of first elements of the selected pair and the minimum among the second elements of the selected pairs. For example, if the following pairs are selected (3, 7), (9, 2) and (2, 5), then the cost will be (3+9+2)*(2) = 28.
Examples:
Input: arr[] = { {4, 7}, {15, 1}, {3, 6}, {6, 8} }, K = 3
Output: 78
The pairs 1, 3 and 4 are selected, therefore the cost = (4 + 3 + 6) * 6 = 78.
Input: arr[] = { {62, 21}, {31, 16}, {19, 2}, {32, 19}, {12, 17} }, K = 4
Output: 2192
Approach: If the second element of a pair is fixed in the answer, then K-1(or less) other pairs are to be selected from those pairs whose second element is greater or equal to the fixed second element and the answer will be maximum if those are chosen such that the sum of first elements is maximum. So, sort the array according to second element and then iterate in descending order taking maximum sum of the first element of K pairs(or less). The maximum sum of first element K pairs can be taken with the help of Set data structure.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Driver function to sort the array elements // by second element of pairs bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { return (a.second < b.second); } // Function that returns the maximum cost of // an array of pairs choosing at most K pairs. int maxCost(pair<int, int> a[], int N, int K) { // Initialize result and temporary sum variables int res = 0, sum = 0; // Initialize Set to store K greatest // element for maximum sum set<pair<int, int> > s; // Sort array by second element sort(a, a + N, sortbysec); // Iterate in descending order for (int i = N - 1; i >= 0; --i) { s.insert(make_pair(a[i].first, i)); sum += a[i].first; while (s.size() > K) { auto it = s.begin(); sum -= it->first; s.erase(it); } res = max(res, sum * a[i].second); } return res; } // Driver Code int main() { pair<int, int> arr[] = { { 12, 3 }, { 62, 21 }, { 31, 16 }, { 19, 2 }, { 32, 19 }, { 12, 17 }, { 1, 7 } }; int N = sizeof(arr) / sizeof(arr[0]); int K = 3; cout << maxCost(arr, N, K); return 0; } |
2000
Recommended Posts:
- Minimum cost to make array size 1 by removing larger of pairs
- Minimum sum by choosing minimum of pairs from array
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Minimum cost of choosing 3 increasing elements in an array of size N
- Find all pairs (a, b) in an array such that a % b = k
- Find number of pairs (x, y) in an array such that x^y > y^x
- Find the sum of all possible pairs in an array of N elements
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Find number of pairs in an array such that their XOR is 0
- Find two non-overlapping pairs having equal sum in an Array
- Find pairs in array whose sums already exist in array
- Find Unique pair in an array with pairs of numbers
- Find k ordered pairs in array with minimum difference d
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



