-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.cpp
57 lines (50 loc) · 1.21 KB
/
activity.cpp
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 <bits/stdc++.h>
using namespace std;
struct Activity
{
int start, finish;
};
// A utility function that is used for sorting
// activities according to finish time
bool activityCompare(Activity s1, Activity s2)
{
return (s1.finish < s2.finish);
}
// Returns count of the maximum set of activities that can
// be done by a single person, one at a time.
void printMaxActivities(Activity arr[], int n)
{
// Sort jobs according to finish time
sort(arr, arr+n, activityCompare);
cout << "Following activities are selected n";
// The first activity always gets selected
int i = 0;
cout << "(" << arr[i].start << ", " << arr[i].finish << "), ";
// Consider rest of the activities
for (int j = 1; j < n; j++)
{
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if (arr[j].start >= arr[i].finish)
{
cout << "(" << arr[j].start << ", "
<< arr[j].finish << "), ";
i = j;
}
}
}
// Driver program
int main()
{
int n;
cout<<"number of works"<<endl;
cin>>n;
Activity a[n];
cout<<"enter input"<<endl;
for(int i=0;i<n;i++){
cin >>a[i].start>>a[i].finish;
}
printMaxActivities(a, n);
return 0;
}