I tried solving this probblem on spoj. http://www.spoj.com/problems/BUSYMAN/
Although I was able to solve it but I got a very strange error. I tried understanding the cause of it but failed. I have two codes.
///////////////////////////////////////////////////
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class activity
{
public:
int start,end;
};
bool comp(activity p, activity q)
{
if(p.end<q.end)return true;
if(p.end==q.end&&p.start<=q.start)return true;
return false;
}
int main()
{
int t;
cin>>t;
vector<activity> v;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
v.resize(n);
for(int j=0;j<n;j++)cin>>v[j].start>>v[j].end;
sort(v.begin(),v.end(),comp);
int ans=0,currend=0;
for(int j=0;j<n;j++)
{
if(v[j].start>=currend){ans++;currend=v[j].end;
}
}
cout<<ans<<endl;
}
}
/////////////////////////////////////////////
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class activity
{
public:
int start,end;
};
bool comp(activity p, activity q)
{
if(p.end<q.end)return true;
if(p.end==q.end&&p.start>=q.start)return true;
return false;
}
int main()
{
int t;
cin>>t;
int n;
vector<activity> v;
for(int i=0;i<t;i++)
{
cin>>n;
v.resize(n);
for(int j=0;j<n;j++)
cin>>v[j].start>>v[j].end;
sort(v.begin(),v.end(),comp);
int ans=0,currend=0;
for(int j=0;j<n;j++)
{
if(v[j].start>=currend)
{
ans++;currend=v[j].end;
}
}
cout<<ans<<endl;
}
}
//////////////////////////////
My problem is that the first one gives segmentation fault on spoj while second one does not. The only difference between the two is the comparison function. I just happen to define the second statement of the comparison function in two different ways which are similar. But it gives me segmentation fault in the first case but not in second case.
In the two images above there are two codes with respective submission ids and in the third it shows seg fault for one while not for other. You can verify with the submission ids on my spoj profile as well.