ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SequenceQueries.h File Reference

Questions on a sequence that a sort or a single scan answers faster than the comparison of all the pairs: the smaller items on the right of each item, the longest run with few distinct items, the longest stretch that a set of intervals leaves uncovered, and the cheapest matching of two sets of points on a line. More...

#include "ADS/Contract.h"
#include "ADS/sort/MergeSort.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
#include <numeric>
#include <optional>
#include <ranges>
#include <set>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/algorithm/impl/SequenceQueries.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<class T, class C = std::less<>>
std::vector< std::size_t > ADS::smallerToTheRight (const std::vector< T > &A, C less=C())
 the counts \( c[ i ] \) of the places \( j > i \) with \( A[ j ] < A[ i ] \), by mergesort on the places; \( O( n \log n ) \)
template<std::ranges::random_access_range R>
std::pair< std::size_t, std::size_t > ADS::longestKDistinctRun (const R &S, std::size_t k)
 the start and the length of the first longest run of consecutive items of \( S \) with at most \( k \) distinct values, by a sliding window; \( O( n \log k ) \)
template<class T>
requires ( std::is_arithmetic_v< T > && ! std::is_same_v< T , bool > )
std::optional< std::pair< T, T > > ADS::largestGap (std::vector< std::pair< T, T > > I)
 the longest stretch \( ( x , y ) \), between the first start and the last end, covered by none of the closed intervals \( I \), the first one on ties, or nothing; \( O( n \log n ) \)
template<class T, class W = T>
requires std::is_arithmetic_v< T >
W ADS::lineAssignment (std::vector< T > H, std::vector< T > L)
 the least sum of \( | H[ i ] - L[ \pi( i ) ] | \) over the permutations \( \pi \), computed in the type W, by sorting; \( O( n \log n ) \)

Detailed description

Questions on a sequence that a sort or a single scan answers faster than the comparison of all the pairs: the smaller items on the right of each item, the longest run with few distinct items, the longest stretch that a set of intervals leaves uncovered, and the cheapest matching of two sets of points on a line.

  • smallerToTheRight(): for each place \( i \), the number of places \( j > i \) with \( A[ j ] < A[ i ] \), by mergesort on the places [see MergeSort.h]: when an item of the left half goes to the output, the items of the right half gone before it are exactly the smaller ones on its right in that half, and the halves below added the others; \( O( n \log n ) \) time and \( O( n ) \) space, against the \( \Theta( n^2 ) \) of the double loop. Equal items do not count, since the left one goes first. The sum of the counts is the number of inversions [see countInversions()]; a Fenwick tree on the ranks gives the same counts in \( O( n \log n ) \) by a scan from the right;
  • longestKDistinctRun(): the first longest run of consecutive items with at most \( k \) distinct values, by a window whose right end advances one place at a time and whose left end advances while the window holds more than \( k \) values, with the counts of its values in a search tree; each end moves \( n \) times, \( O( n \log k ) \);
  • largestGap(): the longest stretch between the first start and the last end of a set of closed intervals \( [ a , b ] \) covered by none of them, the first one on ties: the intervals by start, and a gap wherever a start is beyond the farthest end so far, the union of intervals by a sweep; \( O( n \log n ) \);
  • lineAssignment() (exercise 14.8): the least sum of \( | h_i - l_{\pi( i )} | \) over the matchings \( \pi \) of two sets of \( n \) points on a line, both sorted and matched in order: if \( h_1 \le h_2 \) and \( l_1 \le l_2 \) the crossed matching never costs less than the straight one, an exchange argument; \( O( n \log n ) \), against the \( O( n^3 ) \) of the Hungarian method on general costs. The same order is optimal for any convex function of the difference, the optimal transport on the line.

The contracts [see Contract.h]: the counts of smallerToTheRight() sum to the inversions of \( A \) (an audit); the run of longestKDistinctRun() has at most \( k \) values (an audit); the intervals of largestGap() must have \( a \le b \), and its gap meets none of them (an audit); lineAssignment() needs two sets of the same size. The functions are defined in SequenceQueries.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, exercise 14.8, https://algoritmicamente.it/materiali/libro/capitoli/cap14.pdf, and the Esercizi Divide-et-impera, exercise A3 of 14/06/2024
P. M. Fenwick, A new data structure for cumulative frequency tables, Software: Practice and Experience 24(3), 327-336, 1994, doi:10.1002/spe.4380240306
H. W. Kuhn, The Hungarian method for the assignment problem, Naval Research Logistics Quarterly 2(1-2), 83-97, 1955, doi:10.1002/nav.3800020109
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa