- Related Questions & Answers
- Minimum Cost to Connect Sticks in C++
- Minimum Cost to Merge Stones in C++
- Minimum Cost For Tickets in C++
- C Program for Minimum Cost Path
- Program to find minimum cost to paint fences with k different colors in Python
- Minimum Cost Polygon Triangulation
- Minimum Cost To Make Two Strings Identical in C++
- Find minimum cost to buy all books in C++
- Minimum Cost to make two Numeric Strings Identical in C++
- Minimum Cost to cut a board into squares in C++
- Program to find minimum cost to reach final index with at most k steps in python
- Find minimum adjustment cost of an array in C++
- Connect n ropes with minimum cost
- Connecting Cities With Minimum Cost in Python
- Minimum Cost Tree From Leaf Values in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose there are N workers. Each worker has the quality parameter. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules −

Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group.

Every worker in the paid group must be paid at least their minimum wage expectation.

We have to find the least amount of money needed to form a paid group satisfying the above conditions.

So, if the input is like quality = [10,22,5], wage = [70,52,30] and K = 2, then the output will be 105.000. This is because we will pay 70 to the first worker and 35 to the 3rd worker.

To solve this, we will follow these steps −

Define Data with q, w and r

n := size of quality

Make an array of Data v of size n

for initialize i := 0, when i < n, update (increase i by 1), do −

q of v[i] := quality[i]

w of v[i] := wage[i]

r of v[i] := w of v[i] /q of v[i]

sort the array v based on the r values

temp := 0

sum := 0

ans := inf

Define one priority queue pq

for initialize i := 0, when i < n, update (increase i by 1), do −

if size of pq is same as k, then −

x := top element of pq

sum := sum - x

delete element from pq

if size of pq is same as k - 1, then −

ans := minimum of (sum * r of v[i]) + w of v[i] and ans

sum := sum + q of v[i]

insert q of v[i] into pq

return ans

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; struct Data { double q, w, r; }; class Solution { public: static bool cmp(Data a, Data b) { return a.r < b.r; } double mincostToHireWorkers(vector<int> &quality, vector<int> &wage, int k) { int n = quality.size(); vector<Data> v(n); for (int i = 0; i < n; i++) { v[i].q = quality[i]; v[i].w = wage[i]; v[i].r = v[i].w / v[i].q; } sort(v.begin(), v.end(), cmp); double temp = 0; double sum = 0; double ans = INT_MAX; priority_queue<int> pq; for (int i = 0; i < n; i++) { if (pq.size() == k) { double x = pq.top(); sum -= x; pq.pop(); } if (pq.size() == k - 1) { ans = min((sum * v[i].r) + v[i].w, ans); } sum += v[i].q; pq.push(v[i].q); } return ans; } }; main(){ Solution ob; vector<int> v = {10,22,5}, v1 = {70,52,30}; cout << (ob.mincostToHireWorkers(v, v1, 2)); }

{10,22,5} {70,52,30} 2

105

Advertisements