Negative Numbers

(did you account for negative numbers, for example?). When ordering tasks that include negative values, the greedy algorithm seems to work fine. What is the problem with negative numbers?

Hey, negative numbers in what terms, we are given time so they cannot be negative neither the interval be negative work cannot end before it has started.

I am willing to understand where are u willing to introduce the concept of negative numbers in Activity selection

I agree that there can be no negative numbers, precisely when uploading my code, the observation made by the feedback system for the second test case is if I am taking into account negative numbers and, like you, I ask myself refers. That is my question.

could u elaborate i could not get ur point… like what is ur doubt
tell me the instance when u are considering -ve number i/p

This is the system message after uploading my code. He suggests taking negative numbers into account. I think it is not correct.

send me the code in coding blocks ide
there would be some error in the code
i`ll help u out

#include <iostream

#include <algorithm

using namespace std;

int t,n;
pair<int,int> A[105];

bool comparador(pair<int,int> a, pair<int,int> b){
if (a.second < b.second) return true;
return false;
}

int main() {
cin >> t;
// cout << t << endl;

for (int i=0; i< t; i++) { // para cada tarea a resolver
	cin >> n; // leer número de actividades

// cout << n << endl;
for (int j=0; j< n;j++) {
cin >> A[j].first >> A[j].second;
// cout << A[j].first << " " << A[j].second << endl;
}

	sort(A,A+n,comparador);

    int cuenta=0;
	int previa =0;
	for (int j=0; j< n;j++) {
        
        if ( j == 0){ 
		    //cout << A[j].first << " " << A[j].second << endl;
            previa = j;
            cuenta++;
        }
        else {
            if ( A[previa].second <= A[j].first) {
                //cout << A[j].first << " " << A[j].second << endl;
                previa = j;
                cuenta++;
            }
        }
	}
    cout << cuenta;

}
return 0;

}

just a small mistake u havent added a new line character
cout << cuenta << endl;

will give u the correct answer

You’re right, I didn’t see it. Line break is needed to separate cases.

Thanks for yout help.

1 Like