-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_selection_recursive.cpp
65 lines (61 loc) · 1.84 KB
/
activity_selection_recursive.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
58
59
60
61
62
63
64
65
// Activity selection problem using priority queue and recursive greedy
#include <iostream>
#include <bits/stdc++.h>
#include <ctime>
using namespace std;
void printMaxActivities(priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> p, int k, int n)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q = p;
++k;
if (k < n)
{
auto it = q.top();
int start1 = it.second;
int end1 = it.first;
q.pop();
auto itr = q.top();
int start2 = itr.second;
int end2 = itr.first;
q.pop();
if (start2 >= end1)
{
cout << start2 << ", " << end2 << endl;
q.push(make_pair(end2, start2));
printMaxActivities(q, k, n);
}
else
{
q.push(make_pair(end1, start1));
printMaxActivities(q, k, n);
}
}
}
int main()
{
int n, st, fn;
cout << "Enter the number of activities performed: ";
cin >> n;
vector<int> s, f;
for (int i = 0; i < n; i++)
{
cout << "Enter the start time and finish time of the activity " << i + 1 << ": ";
cin >> st >> fn;
s.push_back(st);
f.push_back(fn);
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 0; i < n; i++)
{
pq.push(make_pair(f[i], s[i]));
}
auto it = pq.top();
int sr = it.second;
int fl = it.first;
cout << "The following activities are selected: " << endl
<< sr << ", " << fl << endl;
int k = 0;
clock_t tstart = clock();
printMaxActivities(pq, k, n);
double time1 = (double)(clock() - tstart) / CLOCKS_PER_SEC;
cout << "Time taken by the algorithm is: " << time1 << endl;
}