Editorial for CCC '15 S3 - Gates


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: FatalEagle

A simple greedy algorithm goes like this:

For each plane, assign it to the gate with the largest number that isn't occupied.

It's not hard to see why this is correct — indeed, intuitively, we're using the "worst" gate we can for each plane (since a gate with a lower number can serve at least every plane a gate with a higher number can).

Implementing this naïvely will result in a complexity of \mathcal{O}(GP), which is enough for the weak data used on the actual CCC. To speed this up, we can use a balanced binary search tree to store free gates. In C++, this is the set data structure, with a final time complexity of \mathcal{O}(P \log G).


Comments


  • 2
    cz52013141834  commented on July 13, 2022, 3:37 p.m.

    Why do we need to use set?


  • -12
    bigboy2  commented on Dec. 10, 2020, 5:48 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 12
      a_sad_javamain  commented on Dec. 10, 2020, 6:48 p.m. edited

      O(P(2logG)) = O(PlogG) since we disregard the constant factor