Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way would be to use a <a href="http://en.wikipedia.org/wiki/Binary_heap" rel="noreferrer">min heap</a> (<a href="http://en.cppreference.com/w/cpp/container/priority_queue" rel="noreferrer"><code>std::priority_queue</code></a> in C++). Here's how you'd do it, assuming you had a <code>MinHeap</code> class. (Yes, my example is in C#. I think you get the idea.)</p> <pre><code>int targetTotal = 3000; int totalWeight = 0; // this creates an empty heap! var myHeap = new MinHeap&lt;Passenger&gt;(/* need comparer here to order by weight */); foreach (var pass in passengers) { if (totalWeight &lt; targetTotal) { // unconditionally add this passenger myHeap.Add(pass); totalWeight += pass.Weight; } else if (pass.Weight &gt; 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) &gt; targetTotal) { var oldPass = myHeap.RemoveFirst(); totalWeight -= oldPass.Weight; } // The heap now contains the passengers who will be thrown overboard. </code></pre> <p>According to the standard references, running time should be proportional to <code>n log k</code>, where <code>n</code> is the number of passengers and <code>k</code> 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.</p> <p>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 <code>n log k</code> works out to a reasonably small number.</p> <p>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.</p> <p>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 <code>Peek</code> is an <code>O(1)</code> operation.</p> <p>For a more information about the performance of heap select and quick select, see <a href="http://blog.mischel.com/2011/10/25/when-theory-meets-practice/" rel="noreferrer">When theory meets practice</a>. 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 <a href="http://en.wikipedia.org/wiki/Selection_algorithm#Introselect" rel="noreferrer">Introselect</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload