We are given N jobs numbered 1 to N. For each activity, let Ti denotes the number of days required to complete the job. For each day of delay before starting to work for job i, a loss of Li is incurred.

We are required to find a sequence to complete the jobs so that overall loss is minimized. We can only work on one job at a time.

If multiple such solutions are possible, then we are required to give the lexicographically least permutation (i.e earliest in dictionary order).

Examples:

Input : L = {3, 1, 2, 4} and T = {4, 1000, 2, 5} Output : 3, 4, 1, 2 Explanation: We should first complete job 3, then jobs 4, 1, 2 respectively. Input : L = {1, 2, 3, 5, 6} T = {2, 4, 1, 3, 2} Output : 3, 5, 4, 1, 2 Explanation: We should complete jobs 3, 5, 4, 1 and then 2 in this order.

Let us consider two extreme cases and we shall deduce the general case solution from them.

- All jobs take same time to finish, i.e Ti = k for all i. Since all jobs take same time to finish we should first select jobs which have large Loss (Li). We should select jobs which have the highest losses and finish them as early as possible.

Thus this is a greedy algorithm. Sort the jobs in descending order based on Li only. - All jobs have the same penalty. Since all jobs have the same penalty we will do those jobs first which will take less amount of time to finish. This will minimize the total delay, and hence also the total loss incurred.

This is also a greedy algorithm. Sort the jobs in ascending order based on Ti. Or we can also sort in descending order of 1/Ti.

From the above cases, we can easily see that we should sort the jobs not on the basis of Li or Ti alone. Instead, we should sort the jobs according to the ratio Li/Ti, in descending order.

We can get the lexicographically smallest permutation of jobs if we perform a stable sort on the jobs. An example of a stable sort is merge sort.

To get most accurate result avoid dividing Li by Ti. Instead, compare the two ratios like fractions. To compare a/b and c/d, compare ad and bc.

`// CPP program to minimize loss using stable sort. ` `#include <iostream> ` `#include <algorithm> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `#define all(c) c.begin(), c.end() ` ` ` `// Each job is represented as a pair of int and pair. ` `// This is done to provide implementation simplicity ` `// so that we can use functions provided by algorithm ` `// header ` `typedef` `pair<` `int` `, pair<` `int` `, ` `int` `> > job; ` ` ` `// compare function is given so that we can specify ` `// how to compare a pair of jobs ` `bool` `cmp_pair(job a, job b) ` `{ ` ` ` `int` `a_Li, a_Ti, b_Li, b_Ti; ` ` ` `a_Li = a.second.first; ` ` ` `a_Ti = a.second.second; ` ` ` `b_Li = b.second.first; ` ` ` `b_Ti = b.second.second; ` ` ` ` ` `// To compare a/b and c/d, compare ad and bc ` ` ` `return` `(a_Li * b_Ti) > (b_Li * a_Ti); ` `} ` ` ` `void` `printOptimal(` `int` `L[], ` `int` `T[], ` `int` `N) ` `{ ` ` ` `vector<job> list; ` `// (Job Index, Si, Ti) ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `int` `t = T[i]; ` ` ` `int` `l = L[i]; ` ` ` ` ` `// Each element is: (Job Index, (Li, Ti) ) ` ` ` `list.push_back(make_pair(i + 1, make_pair(l, t))); ` ` ` `} ` ` ` ` ` `stable_sort(all(list), cmp_pair); ` ` ` ` ` `// traverse the list and print job numbers ` ` ` `cout << ` `"Job numbers in optimal sequence are\n"` `; ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `cout << list[i].first << ` `" "` `; ` ` ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `L[] = { 1, 2, 3, 5, 6 }; ` ` ` `int` `T[] = { 2, 4, 1, 3, 2 }; ` ` ` `int` `N = ` `sizeof` `(L) / ` `sizeof` `(L[0]); ` ` ` `printOptimal(L, T, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Job numbers in optimal sequence are 3 5 4 1 2

Time Complexity: O(N log N)

Space Complexity: O(N)

This article is contributed by **Sayan Mahapatra**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.