forked from Minor-lazer/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovering_segments
More file actions
57 lines (55 loc) · 1.16 KB
/
covering_segments
File metadata and controls
57 lines (55 loc) · 1.16 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
#include <algorithm>
#include <iostream>
#include <climits>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct Segment
{
int start, end;
};
vector<int> optimal_points(vector<Segment> &segments)
{
vector<int> points;
auto compare = [](auto a, auto b) { return a.start < b.start; };
sort(segments.begin(),segments.end(),compare);
priority_queue<Segment, vector<Segment>, function<bool(Segment, Segment)>> pq(compare);
for (auto i : segments)
{
if (pq.empty() || pq.top().end < i.start)
{
pq.emplace(i);
}
else
{
auto t = pq.top();
pq.pop();
Segment s;
s.start = max(i.start, t.start);
s.end = min(i.end, t.end);
pq.emplace(s);
}
}
while (!pq.empty())
{
points.emplace_back(pq.top().end);
pq.pop();
}
return points;
}
int main()
{
int n;
std::cin >> n;
vector<Segment> segments(n);
for (size_t i = 0; i < segments.size(); ++i)
{
std::cin >> segments[i].start >> segments[i].end;
}
vector<int> points = optimal_points(segments);
std::cout << points.size() << "\n";
for (size_t i = 0; i < points.size(); ++i)
{
std::cout << points[i] << " ";
}
}