Activity Selection Problem

#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int>p;
vector<p>v;

bool compare(p A, p B)
{
    return (A.second < B.second);
}

void ActivitySelection()
{
    int n, i, j, start_time, finish_time;

    cout << "Enter Activity Number :: ";
    cin >> n;

    cout << "Enter Start and Finish Time :: " << endl;
    for(i=1;i<=n;i++)
    {
        cin >> start_time >> finish_time;
        v.push_back(p(start_time, finish_time));
    }

    sort(v.begin(), v.end(), compare);

    i = 0;

    cout << endl << "Following Activities Are Selected :: " << endl;
    cout << v[i].first << " " << v[i].second << endl;

    for(j=1;j<n;j++)
    {
        if(v[i].second <= v[j].first)
        {
            cout << v[j].first << " " << v[j].second << endl;
            i = j;
        }
    }
}

int main()
{
    ActivitySelection();

    return 0;
}

Comments