Activity Selection Problem

I Have solved this problem using pair<int,int>.
It is giving me the correct output for the given example.
But at the time of submission it is giving “WRONG ANSWER”…

MY CODE IS …

#include<bits/stdc++.h>

using namespace std;

int main()
{

int t;

cin>>t;

while(t--)
{
	int n;
	cin>>n;

	int a,b;
	cin>>a>>b;

	pair<int,int> p= make_pair(a,b);

	int count = 1;


	for(int i=1;i<n;i++)
	{

	  int a,b;
	  cin>>a>>b;

	  if(a>=p.second)
	  {
	  	count++;
	  	p.first = a;
	  	p.second = b;
	  }
	}

	cout<<count<<endl;
}

}

@srivastavasagar2001 could you please provide me your code in coding blocks IDE

Hi @srivastavasagar2001, you have to sort also according to the finishing time , i.e you need to do the following:-

  1. Sort the activities according to their finishing time
  2. Select the first activity from the sorted array and print it.
  3. Do following for remaining activities in the sorted array.
    …….a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.

that’s all there is to this problem
In case of any doubt feel free to ask :slight_smile:
If you got the answer then mark your doubt as resolved

Okay,
But what is wrong in my code?

I am getting the right output for the example input

as i said earlier you have to sort according to your ending time but you are not sorting
also you should not use your initialize function as you are not taking input from a file

example :-
1
6
1 2
3 4
5 9
8 9
0 6
5 7

your output : 3
actual output :4

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.