-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity Selection Problem using Greedy algorithm.cpp
More file actions
50 lines (39 loc) · 1.47 KB
/
Copy pathActivity Selection Problem using Greedy algorithm.cpp
File metadata and controls
50 lines (39 loc) · 1.47 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define endl cout << '\n'
#define vint vector<int>
#define vll vector<ll>
#define pb push_back
#define S second
#define F first
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define cnte(v, x) count(all(v), (x))
#define wl int t; cin >> t; while(t--)
#define sort(v) sort(v.begin(),v.end())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
vector <int> activitySelectionProblem( vector<pair<int, int>> v){
vector <int> res = {0};
int i = 1, j = 0;
for(; i < v.size(); i++){
if(v[i].F >= v[j].S) {
res.pb(i);
j = i;
}
}
return res;
}
int main() {
/* Let's consider that you have n activities with their start and finish times, the objective
* is to find solution set having maximum number of non-conflicting activities.
* Greedy approach can be used to find the solution since we want to maximize the count of activities that can be executed.
* This approach will greedily choose an activity with earliest finish time at every step, thus yielding an optimal solution.
*/
/* the given activities must be in ascending order according to their finishing time. */
vector <pair<int, int>> v = {{9,11}, {10, 11}, {11, 12}, {12, 14},{13, 15}, {15, 16}};
vector <int> res = activitySelectionProblem(v);
for(auto &i : res) cout << i << ' ';
}