
- 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
Program to find final amount that should be paid to employees based on their performance in C++
Suppose we have two lists of numbers of same length called performance and costs. And we also have another number k. These indicates that each worker i performs at performance[i] level and it takes costs at least costs[i]. We have to find the minimum cost to hire k employees given also that the workers will be paid proportionate to their performance compared to other employees in the group.
So, if the input is like performance = [5, 3, 2] costs = [100, 5, 4] k = 2, then the output will be 10, as we can select emp1 and emp2. They must be paid at least 5 + 4 = 9 amount. But emp1 performs 1.5 times better than the emp2, so he should be paid at least 1.5 * 4 = 6. So in total they will get 6 + 4 = 10.
To solve this, we will follow these steps −
n := size of c
Define an array seq of size n
fill seq with values 0 through n−1
sort the array seq based on these criteria (c[i] * p[j] < c[j] * p[i])
ans := inf, psum := 0
define a priority queue pq
for initialize i := 0, when i < n, update (increase i by 1), do −
idx := seq[i]
insert p[idx] into pq
psum := psum + p[idx]
if size of pq > k, then −
psum := psum − top element of pq
delete top element from pq
if i >= k − 1, then −
ans := minimum of ans and (c[idx] / p[idx] * psum)
return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; double solve(vector<int>& p, vector<int>& c, int k) { int n = c.size(); vector<int> seq(n); for (int i = 0; i < n; ++i) seq[i] = i; sort(seq.begin(), seq.end(), [&](int i, int j) { return c[i] * p[j] < c[j] * p[i]; }); double ans = INT_MAX, psum = 0; priority_queue<int> pq; for (int i = 0; i < n; ++i) { int idx = seq[i]; pq.emplace(p[idx]); psum += p[idx]; if (pq.size() > k) { psum −= pq.top(); pq.pop(); } if (i >= k − 1) ans = min(ans, (double)c[idx] / p[idx] * psum); } return ans; } int main(){ vector<int> performance = {5, 3, 2}; vector<int> costs = {100, 5, 4}; int k = 2; cout << solve(performance, costs, k); }
Input
{5, 3, 2}, {100, 5, 4}, 2
Output
10
- Related Articles
- Program to find minimum amount needed to be paid all good performers in Python
- The Impact of Gender Discrimination on Employees’ Performance
- Mitesh, Mihir, Siddharth and Prashant paid a bill of restaurant unequally. The ratio of amount paid by Mitesh to Mihir is 7:8. Siddharth paid half the amount Mihir paid and Prashant paid half the amount Siddharth paid. If the bill amount is Rs. 2100, find the amount paid by each.
- Program to sort out phrases based on their appearances in Python
- C++ Program to find out number of employees of different skill level to be transferred
- Program to sort numbers based on 1 count in their binary representation in Python
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- Program to find maximum units that can be put on a truck in Python
- Program to check two trees are exactly same based on their structure and values in Python
- Program to Find Out the Amount of Rain to be Caught between the Valleys in C++
- How to get all the services based on their status in PowerShell?
- How to select data frame columns based on their class in R?
- How to select data.table object columns based on their class in R?
