-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathActivity_process.cpp
31 lines (31 loc) · 1.03 KB
/
Activity_process.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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
int s,e;
vector<pair<int,int>>activity;//Pair of end time,start time is inserted into vector
for(int i=0;i<n;i++)
{
cin>>s>>e;//Input start time and end time
activity.push_back(make_pair(e,s));
}
//sort the activities according to their finishing time so that we always consider the next activity as the minimum finishing time activity
sort(activity.begin(),activity.end());//Sorting the activity according to end time (Finishing time)
int current_end=-1;
int count=0;
for(int i=0;i<n;i++)
{
//If the start time of this activity is greater than or equal to the finish time of the previously selected activity then increment the count of number of processes that will execute
if(activity[i].second>current_end)
{
count++;
current_end=activity[i].first;
}
}
cout<<count;//maximum number of processes executed
return 0;
}