-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityselectionusingDP.cpp
50 lines (49 loc) · 1.06 KB
/
ActivityselectionusingDP.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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Pair{
public:
int start;
int finish;
};
class Sol{
public:
int finish; //last finish time before ith element
int count;
};
int compareX(Pair job1, Pair job2)
{
return job1.finish < job2.finish;
}
void selector(vector<Pair> jobs)
{
sort(jobs.begin(),jobs.end(), compareX);
vector<Sol> A; //stores number of jobs compatiles before i
Sol temp;
temp.finish= jobs[0].finish;
temp.count=1;
A.push_back(temp);
for(int i=1 ; i< jobs.size() ; i++)
{
if(jobs[i].start >= A[i-1].finish)
{
temp.finish=jobs[i].finish;
temp.count= A[i-1].count +1;
}
else
{
temp.finish= A[i-1].finish;
temp.count= A[i-1].count;
}
A.push_back(temp);
}
for(int i=0 ; i<A.size() ; i++)
cout<<A[i].count<<" ";
}
int main()
{
vector <Pair> jobs= { {1,2},{3,4},{0,6},{5,7},{8,9},{5,9}};
selector(jobs);
return 0;
}