-
Notifications
You must be signed in to change notification settings - Fork 1
/
P20_Activity Selection.cpp
40 lines (35 loc) · 1002 Bytes
/
P20_Activity Selection.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
#include <iostream>
#include <sstream>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
bool finishCompare(pair<int,int> &a, pair<int,int> &b)
{
return a.second < b.second;
}
int main()
{
vector<pair<int, int>> vec;
cout << "Input all activities start and finish time in one line separated by spaces" <<endl;
string buf;
int first, second;
getline(cin, buf);
istringstream str(buf);
while (str >> first >> second) {
vec.push_back(make_pair(first, second));
}
sort(vec.begin(),vec.end(), finishCompare);
vector<pair<int, int>>selectedActivity;
selectedActivity.push_back(vec[0]);
int i=0;
for (auto it = vec.begin(); it != vec.end(); it++)
{
if(it->first >=selectedActivity[i].second)
{
i++;
selectedActivity.push_back(*it);
}
}
for (auto it = selectedActivity.begin(); it != selectedActivity.end(); it++)
cout << "{ "<<it->first << ", " << it->second << " }"<< endl;
}