|
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: 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 ) \) | |
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.
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.