6 Nov 2026
Nearest Passbands (QRT)
What does nth_element do
Imagine sorting the gaps [8, 1, 5, 2, 9]. For k = 2, index 2 would hold 5. nth_element puts 5 there. One legal arrangement is [2, 1, 5, 9, 8]. The two smallest gaps are already in the first two slots. Sorting just those slots gives [1, 2].
auto bands = bands_c; // mutable copy of the const input
// k is already clamped to [0, bands.size()]
auto cut = bands.begin() + k;
std::nth_element(bands.begin(), cut, bands.end(), cmp);
std::sort(bands.begin(), cut, cmp);
cut is the first excluded position. cmp ranks whole bands by gap, then width, then slot. Selection takes O(N) comparisons on average. Sorting the winners takes O(k log k).
Why can a struct have width() const
struct Passband {
int slot, low, high;
int width() const { return high - low; }
};
A C++ struct can have member functions, just like a class. Its members are public by default. band.width() computes the width on demand. The trailing const applies to the band you called it on, so width() can read high and low and can be called on a const Passband&.
How does the heap know which band to evict
auto lesser = [calculator](const Passband& a, const Passband& b) {
return calculator(a) < calculator(b);
};
std::priority_queue<Passband, std::vector<Passband>, decltype(lesser)> heap(lesser);
< gives a max-heap. With gaps 2 and 5, top() gives 5. Once the heap is full, lesser(candidate, heap.top()) means the candidate beats the worst retained band. Reverse < to > for a min-heap. Equal keys must compare false.
decltype(lesser) supplies the comparator type. heap(lesser) supplies the actual object holding its captures. [wavelength] copies the wavelength and [&wavelength] refers to the original. Use k to limit the heap size.
What made the timed code expensive
return std::array<int, 3>{gap, width, band.slot}; gives the same left-to-right ordering as a three-element vector, with fixed inline storage. Your calculator(a) < calculator(b) built two temporary vectors per comparison. The competitor compares integers directly and computes widths only on a gap tie.
std::clog << band.low << ' ' << band.high << '\n'; needs << before each item. std::endl also flushes. Remove the logging loop before timing. The reported 5× alone cannot separate logging, allocation and algorithm costs.
Which edits survive a loop
for (Passband band : bands) copies each element. for (const auto& band : bands) reads the original. for (auto& band : bands) lets you edit the original when bands is mutable. Sorting also writes to the elements, hence the competitor’s auto bands = bands_c;.
int gap = 0;
if (wavelength < band.low) gap = band.low - wavelength;
else if (wavelength > band.high) gap = wavelength - band.high;
Declare gap before the branches so you can use it afterward. In C++, low <= w <= high compares the Boolean result of low <= w with high. Write low <= w && w <= high for an explicit interval check.
How do I return slots in best first order
std::vector<int> ans(heap.size());
while (!heap.empty()) {
ans[heap.size() - 1] = heap.top().slot;
heap.pop();
}
return ans;
The worst band comes out first, so write backward. Three bands give indices 2, 1, 0. Size from the actual heap to handle fewer than k bands. At function entry, if (k <= 0) return {}; guards the empty-heap case. An unconditional return {}; exits before any loop runs.
If the vector itself is the heap: append with v.push_back(x) then call std::push_heap(v.begin(), v.end(), cmp). To remove the top, call std::pop_heap(v.begin(), v.end(), cmp) then v.pop_back(). std::sort_heap(v.begin(), v.end(), cmp) gives sorted output when needed.