
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Cost to Hire K Workers in C++
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 −
Example
#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)); }
Input
{10,22,5} {70,52,30} 2
Output
105
- Related Articles
- Program to find minimum cost to hire k workers in Python
- Program to find minimum cost to paint fences with k different colors in Python
- Program to find minimum cost to reach final index with at most k steps in python
- Minimum Cost to Connect Sticks in C++
- Minimum Cost to Merge Stones in C++
- Minimum Cost Polygon Triangulation
- Minimum Cost For Tickets in C++
- Minimum Cost To Make Two Strings Identical in C++
- Find minimum cost to buy all books in C++
- Program to find minimum cost to merge stones in Python
- Minimum Cost to make two Numeric Strings Identical in C++
- Minimum Cost to cut a board into squares in Python
- Minimum Cost to cut a board into squares in C++
- Program to find minimum cost for painting houses in Python
- Program to find minimum cost to connect all points in Python
