forked from Yas1h/hacktober22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodead.cpp
More file actions
58 lines (47 loc) · 1.29 KB
/
codead.cpp
File metadata and controls
58 lines (47 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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;
}