forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weighted Job Scheduling.cpp
116 lines (84 loc) · 1.98 KB
/
Weighted Job Scheduling.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
You are given N jobs where every job is represented as:
1.Start Time
2.Finish Time
3.Profit Associated
Find the maximum profit subset of jobs such that no two jobs in the subset overlap.
Input
The first line of input contains one integer denoting N.
Next N lines contains three space separated integers denoting the start time, finish time and the profit associated with the ith job.
Output
Output one integer, the maximum profit that can be achieved.
Constraints
1 ≤ N ≤ 10^6
1 ≤ ai, di, p ≤ 10^6
Sample Input
4
3 10 20
1 2 50
6 19 100
2 100 200
Sample Output
250
*/
//Code
#include <iostream>
#include <algorithm>
using namespace std;
struct Job
{
int start, finish, profit;
};
bool jobComparataor(Job s1, Job s2)
{
return (s1.finish < s2.finish);
}
int latestNonConflict(Job arr[], int index)
{
int lo = 0, hi = index- 1;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
if (arr[mid].finish <= arr[index].start)
{
if (arr[mid + 1].finish <= arr[index].start)
lo = mid + 1;
else
return mid;
}
else
hi = mid - 1;
}
return -1;
}
long long findMaxProfit(Job arr[], int n)
{
sort(arr, arr+n, jobComparataor);
long long *table = new long long[n];
table[0] = arr[0].profit;
for (int i=1; i<n; i++)
{
long long inclProf = arr[i].profit;
int l = latestNonConflict(arr, i);
if (l != -1)
inclProf += table[l];
table[i] = max(inclProf, table[i-1]);
}
long long result = table[n-1];
return result;
}
int main()
{
int n,i,start,finish,profit;
cin>>n;
Job arr[n];
for(i=0;i<n;i++)
{
cin>>start>>finish>>profit;
arr[i].start=start;
arr[i].finish=finish;
arr[i].profit=profit;
}
cout<<findMaxProfit(arr, n);
return 0;
}