Throwing the fattest people off of an overloaded airplane.

后端 未结 12 1728
迷失自我
迷失自我 2020-12-04 04:42

Let\'s say you\'ve got an airplane, and it is low on fuel. Unless the plane drops 3000 pounds of passenger weight, it will not be able to reach the next airport. To save t

12条回答
  •  既然无缘
    2020-12-04 05:26

    One way would be to use a min heap (std::priority_queue in C++). Here's how you'd do it, assuming you had a MinHeap class. (Yes, my example is in C#. I think you get the idea.)

    int targetTotal = 3000;
    int totalWeight = 0;
    // this creates an empty heap!
    var myHeap = new MinHeap(/* need comparer here to order by weight */);
    foreach (var pass in passengers)
    {
        if (totalWeight < targetTotal)
        {
            // unconditionally add this passenger
            myHeap.Add(pass);
            totalWeight += pass.Weight;
        }
        else if (pass.Weight > myHeap.Peek().Weight)
        {
            // If this passenger is heavier than the lightest
            // passenger already on the heap,
            // then remove the lightest passenger and add this one
            var oldPass = myHeap.RemoveFirst();
            totalWeight -= oldPass.Weight;
            myHeap.Add(pass);
            totalWeight += pass.Weight;
        }
    }
    
    // At this point, the heaviest people are on the heap,
    // but there might be too many of them.
    // Remove the lighter people until we have the minimum necessary
    while ((totalWeight - myHeap.Peek().Weight) > targetTotal)
    {
        var oldPass = myHeap.RemoveFirst();
        totalWeight -= oldPass.Weight; 
    }
    // The heap now contains the passengers who will be thrown overboard.
    

    According to the standard references, running time should be proportional to n log k, where n is the number of passengers and k is the maximum number of items on the heap. If we assume that passengers' weights will typically be 100 lbs or more, then it's unlikely that the heap will contain more than 30 items at any time.

    The worst case would be if the passengers are presented in order from lowest weight to highest. That would require that every passenger be added to the heap, and every passenger be removed from the heap. Still, with a million passengers and assuming that the lightest weighs 100 lbs, the n log k works out to a reasonably small number.

    If you get the passengers' weights randomly, performance is much better. I use something quite like this for a recommendation engine (I select the top 200 items from a list of several million). I typically end up with only 50,000 or 70,000 items actually added to the heap.

    I suspect that you'll see something quite similar: the majority of your candidates will be rejected because they're lighter than the lightest person already on the heap. And Peek is an O(1) operation.

    For a more information about the performance of heap select and quick select, see When theory meets practice. Short version: if you're selecting fewer than 1% of the total number of items, then heap select is a clear winner over quick select. More than 1%, then use quick select or a variant like Introselect.

提交回复
热议问题