ChaituBoi's blog

By ChaituBoi, history, 4 years ago, In English

So i have almost similar approach to the editorial, where i chose the smallest feasible day for every exam and update my answer. Though i am getting RUNTIME_ERROR in test case 18.

Things i checked for: - proper data types used - dry ran my code on paper on many cases - searched for meaning of exit code -1073741819, but to no avail. still get the error.

The compiler does not show the full test case and stops in between so cannot give u the test case. But it is numbered 18 with very large values.

Here is my code:

#include<bits/stdc++.h>
using namespace std;
bool compare(pair<long long,long long> &a,pair<long long,long long> &b)
{
	if(a.first<=b.first)
		return true;
	return false;
}
int main()
{
	int n;
	cin>>n;
	pair<long long,long long> A[n];
	long long day=0;
	for(int i=0;i<n;i++)
	{
		long long a,b;
		cin>>a>>b;
		A[i]={a,b};
	}
	sort(A,A+n,compare);
	for(int i=0;i<n;i++)
	{
		long long temp=LLONG_MAX;
		if(day<=A[i].first)
			temp=A[i].first;
		if(day<=A[i].second && temp>A[i].second)
			temp=A[i].second;
		if(temp!=LLONG_MAX)	
			day=temp;	
	}
	cout<<day;				
	return 0;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

ISO C++ Standard (N4659) section 28.7:

3. For all algorithms that take Compare, there is a version that uses operator< instead. That is, comp(*i, *j) != false defaults to *i < *j != false. For algorithms other than those described in 28.7.3, comp shall induce a strict weak ordering on the values.
4. The term strict refers to the requirement of an irreflexive relation (!comp(x, x) for all x), and the term weak to requirements that are not as strong as those for a total ordering, but stronger than those for a partial ordering. If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations:
(4.1) — comp(a, b) && comp(b, c) implies comp(a, c)
(4.2) — equiv(a, b) && equiv(b, c) implies equiv(a, c)

Your compare violates this requirement.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it