-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathWeightedjob.cpp
107 lines (86 loc) · 2.06 KB
/
Weightedjob.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
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
PROBLEM STATEMENT
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
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct job{
ll start, finish, profit;
job(ll s, ll f, ll p){
start = s;
finish = f;
profit = p;
}
};
bool compare(job a, job b){
return a.finish < b.finish;
}
ll search(vector<job> *input, ll limit, ll si, ll ei){
if(si > ei) return -1;
if(si == ei){
if((input->at(si)).finish <= limit) return si;
else return -1;
}
ll mid = (si+ei)/2;
if((input->at(mid)).finish <= limit){
ll answer = search(input, limit, mid+1, ei);
if(answer == -1) return mid;
else return answer;
}
else return search(input, limit, si, mid-1);
}
int main(){
ll n;
cin >> n;
vector<job> input;
for(ll i = 0; i < n; i++){
ll s, f, p;
cin >> s >> f >> p;
input.push_back(job(s, f, p));
}
sort(input.begin(), input.end(), compare);
ll *dp = new ll[n];
dp[0] = input[0].profit;
for(ll i = 1; i < n; i++){
ll include = input[i].profit;
ll id = -1;
id = search(&input, input[i].start, 0, i-1);
// for(ll j = i-1; j >= 0; j--){
// if(input[j].finish <= input[i].start){
// id = j;
// break;
// }
// }
if(id != -1){
include += dp[id];
}
dp[i] = max(dp[i-1], include);
}
cout << dp[n-1] << endl;
}