C++ Program to find out number of employees of different skill level to be transferred


Suppose, there are n employees in a company. Each of the employees is given a rank based on their skills. The ranks are numbered from 1 to k. The number of employees having a rank i is given in the array skill, where skill[i] represents the number of employees having rank i. Now, a new branch of the company has opened and employees of different skills have to be transported to that branch. The number of employees to be sent to that branch is m. We have to find a way so that we can transfer m employees of different skill to that new branch. We have to minimize the following formula to get the branch's employee rank allocation table branch.

The formula is: max(branch[i]/m - skill[i]/n).

The summation of values of branch[i] results in m. We have to find out the elements in branch[i].

So, if the input is like k = 5, n = 10, m = 25, skill = {5, 3, 2, 7, 4}, then the output will be 12 7 5 17 10.

Steps

To solve this, we will follow these steps −

sum := 0
for initialize i := 0, when i < k, update (increase i by 1), do:
   skill[i] := skill[i] * m / n
Define an array a containing integer pairs
for initialize i := 0, when i < k, update (increase i by 1), do:
   c := skill[i]
   sum := sum + c
   first value of a[i] := skill[i] - c
   second value of a[i] := i
sort the array a
reverse the array a
for initialize i := 0, when i < m - sum, update (increase i by 1), do:
   skill[second value of a[i]] := skill[second value of a[i]] + 1
for initialize i := 0, when i < k, update (increase i by 1), do:
   if i is not equal to k - 1, then:
      print(skill[i])
   Otherwise,
      print(skill[i])

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

void solve(int k, int n, int m, vector<double> skill){
   int sum = 0;
   for (int i = 0; i < k; i++)
      skill[i] = skill[i] * m / n;
   vector<pair<double, int>> a(k);
   for (int i = 0; i < k; i++) {
      int c = skill[i];
      sum += c;
      a[i].first = skill[i] - c;
      a[i].second = i;
   }
   sort(a.begin(), a.end());
   reverse(a.begin(), a.end());
   for (int i = 0; i < m - sum; i++) {
      skill[a[i].second] += 1;
   }
   for (int i = 0; i < k; i++) {
      if (i != k - 1)
         cout << int(skill[i]) << " ";
      else
         cout << int(skill[i]) << endl;
   }
}
int main() {
   int k = 5, n = 10, m = 25;
   vector<double> skill = {5, 3, 2, 7, 4};
   solve(k, n, m, skill);
   return 0;
}

Input

5, 10, 25, {5, 3, 2, 7, 4}

Output

12 7 5 17 10

Updated on: 02-Mar-2022

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements