Created
August 30, 2018 06:56
-
-
Save gallirohik/0ea2c532458582df6e92bd4bd9b13f4a to your computer and use it in GitHub Desktop.
Activity selection problem using Greedy approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<utility> | |
#include<vector> | |
#include<algorithm> | |
using namespace std; | |
bool myfun(pair<int,int> a ,pair<int,int> b) | |
{ | |
return a.second < b.second; | |
} | |
void getActivities(vector< pair<int,int> > a) | |
{ | |
sort(a.begin(),a.end(),myfun); | |
int i=0; | |
cout<<a[0].first<<"--"<<a[0].second<<" "<<endl; | |
for(int j=1;j<a.size();j++) | |
{ | |
if(a[j].first>=a[i].second) | |
{ | |
cout<<a[j].first<<"--"<<a[j].second<<" "<<endl; | |
i=j; | |
} | |
} | |
} | |
int main() | |
{ | |
vector<pair <int,int> > a; | |
pair<int,int> p; | |
cout<<"Enter No of Activities:"<<endl; | |
int n,st,ed; | |
cin>>n; | |
cout<<"Enter Activities:"<<endl; | |
for(int i=0;i<n;i++) | |
{ | |
cin>>st>>ed; | |
p.first=st; | |
p.second=ed; | |
a.push_back(p); | |
} | |
getActivities(a); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment