-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSegment.cpp
More file actions
85 lines (75 loc) · 1.92 KB
/
DisjointSegment.cpp
File metadata and controls
85 lines (75 loc) · 1.92 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
74
75
76
77
78
79
80
81
82
83
84
85
/*
Problem: Disjoint Segment
Description
Given a set of segments X = {(a_1, b_1),.., (a_n, b_n)} in which a_i < b_i are coordinates
of the segment i on a line, i = 1, …, n. Find a subset of X having largest cardinality in
which no two segments of the subset intersect.
Input
Line 1: contains a positive integer n (1 <= n <= 100000)
Line i+1 (i= 1,...,n): contains ai and bi (0 <= a_i <= b_i <= 1000000)
Output
Number of segments in the solution found.
Example
Input
6
0 10
3 7
6 14
9 11
12 15
17 19
Output
4
*/
#include<bits/stdc++.h>
using namespace std;
struct compareSegment
{
bool operator()(pair<int, int> segment1, pair<int, int> segment2) const {
return segment1.second <= segment2.second;
}
};
set<pair<int, int>, compareSegment> segments;
int n;
int res; // number of disjoint segment
int l; //lastest-segment.second
void input() {
cin >> n;
int a, b;
for(int i = 0; i < n; i++) {
cin >> a >> b;
segments.insert(make_pair(a, b));
}
// for (const pair<int, int> segment : segments)
// {
// cout << segment.first << ", " << segment.second << endl;
// }
}
void solve() {
/*
res = 1;
l = segments.begin.second
segments remove first
duyệt các phần tử segment còn lại trong segment
Nếu segment.first >= l thì
res++
l = segment.second
*/
res = 0;
l = -1; // Initial value for the last segment end
while (!segments.empty()) {
auto it = segments.begin();
pair<int, int> currentSegment = *it;
if (currentSegment.first > l) { // không được xét điều kiện currentSegment.first >= l, bởi như vậy hai đoạn đã giao nhau tại vị trí điểm l rồi
res++;
l = currentSegment.second;
}
segments.erase(it);
}
}
int main() {
input();
solve();
cout << res << endl;
return 0;
}