-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjects.cpp
More file actions
37 lines (30 loc) · 1.13 KB
/
Projects.cpp
File metadata and controls
37 lines (30 loc) · 1.13 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<long long>> projects(n);
vector<long long> end_times(n);
for (int i = 0; i < n; i++) {
projects[i].resize(3);
cin >> projects[i][0] >> projects[i][1] >> projects[i][2];
end_times[i] = projects[i][1];
}
// Sort projects by end time
sort(projects.begin(), projects.end(),
[](const vector<long long>& a, const vector<long long>& b) {
return a[1] < b[1];
});
sort(end_times.begin(), end_times.end());
// dp[i] represents max profit considering projects [0...i]
vector<long long> dp(n + 1, 0);
for (int i = 0; i < n; i++) {
// Find rightmost project that ends before current project starts
int prev_project = lower_bound(end_times.begin(), end_times.end(),
projects[i][0]) - end_times.begin();
// Take maximum of including or excluding current project
dp[i + 1] = max(dp[i], dp[prev_project] + projects[i][2]);
}
cout << dp[n] << endl;
return 0;
}