Need help in Codeforces Problem B (Div 3)

Problem link: Problem - B - Codeforces
Submission: Submission #69418039 - Codeforces

Where am I going wrong?

1 Like

You need to sort the input first with respect to x then with respect to y.

1 Like

Figured it out finally. Wasn’t sorting it correctly.

a suggestion when you are sorting use a better comparison function.
though i have a doubt how are you taking the array as [(x0,y0],[x1,y1]…]?
and yeah when you sorted according to the first index it ignored the second index.
i know in C++ if you sort a vector of pairs it will sort the first one then it will sort according to the second if first is similar.
I think it applies the same in C++ that’s why normal sorting works too.

Yep, exactly. I assumed that this happens in python too, but to my surprise it doesn’t (or maybe not in the way I did), and that’s where I was wrong.

dude i did the normal sorting and it worked

so what i was saying was when you do

    packages = sorted(packages, key=lambda x: x[1])

you are sorting according to the second index.(zero based indexing)
while

    packages = sorted(packages)

sorts first index then looks at the second index :slight_smile:
that’s why you got WA in that.

I did use that, but it verdict was failed pretest 3. So I get your point.

If I sort according to second index, it doesn’t take care of the first? Is that what you’re saying?

yes it will ignore the first index.

what you might be trying to do was this

sorry it’s in C++ but see the bool comp.

Yea I can see it, you’ve created that compartor for this purpose.

1 Like