-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1140_Projects.cpp
More file actions
73 lines (58 loc) · 1.72 KB
/
1140_Projects.cpp
File metadata and controls
73 lines (58 loc) · 1.72 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <chrono>
#include <vector>
#include <unordered_map>
using namespace std;
using namespace std::chrono;
struct Project
{
int start;
int end;
int value;
};
long max_value(vector<Project> &projects)
{
auto cmp = [projects](auto p1, auto p2)
{
return p1.start < p2.start;
};
sort(begin(projects), end(projects), cmp);
unordered_map<int, long> dp_max_value;
vector<int> project_starts;
for (const auto &project : projects)
if (project_starts.empty() || project_starts.back() != project.start)
project_starts.push_back(project.start);
long max_value = 0;
for (auto itr = projects.rbegin(); itr != projects.rend(); ++itr)
{
int start = itr->start;
if (!dp_max_value.contains(start))
dp_max_value[start] = max_value;
long max_next_project_value = 0;
auto itr_next_project_start = upper_bound(project_starts.begin(), project_starts.end(), itr->end);
if (itr_next_project_start != project_starts.end())
max_next_project_value = dp_max_value[*itr_next_project_start];
dp_max_value[start] = max(dp_max_value[start], itr->value + max_next_project_value);
max_value = max(max_value, dp_max_value[start]);
}
return max_value;
}
int main()
{
auto start_time = high_resolution_clock::now();
int num_projects;
scanf("%d", &num_projects);
vector<Project> projects;
projects.reserve(num_projects);
for (int i = 0; i < num_projects; ++i)
{
int start, end, value;
scanf("%d %d %d", &start, &end, &value);
projects.emplace_back(start, end, value);
}
cout << max_value(projects) << '\n';
auto end_time = high_resolution_clock::now();
auto duration_ms = duration_cast<milliseconds>(end_time - start_time);
cerr << "Execution time: " << duration_ms.count() << " ms" << '\n';
return 0;
}