-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivitySelection.cpp
81 lines (70 loc) · 1.53 KB
/
ActivitySelection.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void Activity(vector<int>& s, vector<int>& f, int k, int n, vector<string>& Aset) {
// find a compatible acitivity with earlist finishing time(greedy chioce)
int m = k + 1;
while (s[m] < f[k] && n >= m ) {
m = m + 1;
}
if (m <= n) {
Aset.push_back("a" + std::to_string(m));
Activity(s, f, m, n, Aset);
return;
}
return;
}
void Activity_iterative(vector<int>& s, vector<int>& f, int n, vector<string>& Aset) {
Aset.push_back("a1");
int k = 1;
for (int m = 2; m <= n; ++m) {
if (s[m] >= f[k]) {
Aset.push_back("a" + std::to_string(m));
k = m;
}
}
}
int main() {
vector<int> s,f;
int n;
cout << "Enter the num of activities: " ;
cin >> n;
// n = 11
cout << "Insert Input arrays " << endl;
// Assume that inputs are already sorted : f[1] <= f[2] <= f[3] <= ... <= f[11]
/*
starting time and finishing time:
11
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
*/
int input;
for (int i = 0; i < n; ++i) {
// starting time
cin >> input;
s.push_back(input);
}
// sentinel value
s.insert(s.begin(), -1);
s.push_back(1000);
for (int i = 0; i < n; ++i) {
// finishing time
cin >> input;
f.push_back(input);
}
// sentinel value
f.insert(f.begin(), 0);
f.push_back(-1);
vector<string> Aset;
Activity(s, f, 0, n, Aset);
for (string a : Aset) {
cout << a << " ";
}cout << endl;
vector<string> Aset2;
Activity_iterative(s, f, n, Aset2);
for (string a : Aset2) {
cout << a << " ";
}cout << endl;
return 0;
}