-
Notifications
You must be signed in to change notification settings - Fork 18
/
code_BinarySearch(Projects).cpp
76 lines (56 loc) · 1.5 KB
/
code_BinarySearch(Projects).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
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
struct job
{
ll start,finish,money;
};
bool comp(job j1,job j2)//comparator func used to sort according to the finish times.
{
return (j1.finish < j2.finish);
}
int find(job a[],ll ind)//Find the latest job that comes before the job no. i.
{
ll l = 0,h = ind-1;
while(l <= h)//Binary Search
{
ll mid = (l+h)/2;
if(a[mid].finish < a[ind].start)
{
if(a[mid+1].finish < a[ind].start)
l = mid + 1;
else
return mid;
}
else
h = mid - 1;
}
return -1;
}
int main()
{
//Fast io.
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n;
cin>>n;
struct job a[n];
for(ll i=0;i<n;i++) //taking input
cin>>a[i].start>>a[i].finish>>a[i].money;
sort(a,a+n,comp);// sort based on finish time
ll *dp = new ll[n];
dp[0] = a[0].money;
for(ll i=1;i<n;i++)
{
ll temp = a[i].money; //temp = stores money of project i.
ll search = find(a,i);
if(search != -1)
temp += dp[search];
dp[i] = max(dp[i-1],temp); //store the max between the dp[i-1] and temp
//we have basically stored the max between 2 cases. i) if project i is included. ii) if project i is excluded.
}
cout<<dp[n-1]<<endl;
delete[] dp;
return 0;
}