|
ADS
Algorithms and Data Structures
|
Questions on a sequence that a sort or a single scan answers faster than the comparison of all the pairs. More...
Files | |
| file | ADS/algorithm/SequenceQueries.h |
| 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. | |
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 ) \) | |
Questions on a sequence that a sort or a single scan answers faster than the comparison of all the pairs.
The definitions, the invariants and the costs of the area are in Theory of other algorithms.
| 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 ) \)
In pseudocode (the sweep of the line, beyond the book), the sweep of the intervals by start:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{largestGap}( I ) \cr 2 & \quad \text{sort } I \text{ by start};\ g \gets \textbf{nil} \cr 3 & \quad e \gets \text{the end of the first interval}\quad \triangleright\ \text{the farthest end so far} \cr 4 & \quad \textbf{foreach}\ [ a , b ] \in I\ \textbf{do} \cr 5 & \qquad \textbf{if}\ e < a\ \textbf{and}\ ( g = \textbf{nil} \ \textbf{or}\ a - e > |g| )\ \textbf{then}\ g \gets ( e , a ) \cr 6 & \qquad e \gets \max\{ e , b \} \cr 7 & \quad \textbf{return}\ g \end{array}} \]
| 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 ) \)
In pseudocode (exercise 14.8 of the book), the matching in order being optimal by an exchange argument:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lineAssignment}( H , L )\quad \triangleright\ \text{the } i \text{-th smallest with the } i \text{-th smallest} \cr 2 & \quad \text{sort } H \text{ and } L;\ \textbf{return} \ \textstyle\sum_{i=0}^{n-1} | H[ i ] - L[ i ] | \end{array}} \]
| 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 ) \)
In pseudocode (the sliding window, beyond the book), \( N \) a search tree of the counts of the values in the window:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestKDistinctRun}( S[ 0 .. n - 1 ] , k ) \cr 2 & \quad s \gets 0;\ \ell \gets 0;\ b \gets 0;\ N \gets \emptyset \cr 3 & \quad \textbf{for}\ e \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad N[ S[ e ] ] \gets N[ S[ e ] ] + 1 \cr 5 & \qquad \textbf{while}\ N \text{ has more than } k \text{ values} \ \textbf{do} \cr 6 & \qquad\quad N[ S[ s ] ] \gets N[ S[ s ] ] - 1 \text{, removed at 0}; \ s \gets s + 1 \cr 7 & \qquad \textbf{if}\ e + 1 - s > \ell\ \textbf{then}\ \ell \gets e + 1 - s;\ b \gets s \cr 8 & \quad \textbf{return}\ ( b , \ell ) \end{array}} \]
| 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 ) \)
In pseudocode (exercise A3 of 14/06/2024, Smaller Count, of the Esercizi Divide-et-impera of the book), the bottom-up mergesort of the places \( P \), the runs of width \( w \) merged in pairs:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{smallerToTheRight}( A[ 0 .. n - 1 ] )\quad \triangleright\ \text{a bottom-up merge sort of the indices} \cr 2 & \quad c \gets ( 0 , \ldots , 0 );\ P \gets ( 0 , 1 , \ldots , n - 1 ) \cr 3 & \quad \textbf{for}\ w \gets 1 , 2 , 4 , \ldots \text{ while } w < n \ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ \text{two neighboring runs } P[ \mathit{lo} .. m - 1 ] , P[ m .. \mathit{hi} - 1 ] \text{ of length } w \ \textbf{do} \cr 5 & \qquad\quad \text{merge them by the values in } A \text{, the left run first on a tie} \cr 6 & \qquad\quad \text{and, as each } P[ i ] \text{ of the left run is output, add to } c[ P[ i ] ] \cr & \qquad\qquad \text{the number of the items of the right run already output} \cr 7 & \quad \textbf{return}\ c \end{array}} \]