-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivitySelection.cpp
More file actions
77 lines (56 loc) · 1.37 KB
/
activitySelection.cpp
File metadata and controls
77 lines (56 loc) · 1.37 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
// Activity Selection
// Send Feedback
// You are given n activities with their start and finish times. Select the maximum number of activities that can be
// performed by a single person, assuming that a person can only work on a single activity at a time.
// Input
// The first line of input contains one integer denoting N.
// Next N lines contains two space separated integers denoting the start time and finish time for the ith activity.
// Output
// Output one integer, the maximum number of activities that can be performed
// Constraints
// 1 ≤ N ≤ 10^6
// 1 ≤ ai, di ≤ 10^9
// Sample Input
// 6
// 1 2
// 3 4
// 0 6
// 5 7
// 8 9
// 5 9
// Sample Output
// 4
#include<bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> p1, pair<int, int> p2) {
return p1.second <= p2.second;
}
int getMaxActivities(pair<int, int> *ar, int n) {
sort(ar, ar+n, compare);
// for(int i = 0; i < n; i++) {
// cout << ar[i].first << " " << ar[i].second << endl;
// }
int ans = 1;
int prev = 0;
for(int i = 1; i < n; i++) {
if(ar[i].first >= ar[prev].second) {
ans++;
prev = i;
}
}
return ans;
}
int main() {
int n;
cin >> n;
pair<int, int> *ar = new pair<int, int>[n];
for(int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
ar[i] = make_pair(x, y);
}
int ans = getMaxActivities(ar, n);
cout << ans << endl;
delete [] ar;
return 0;
}