ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SequenceProblems.h

Problems of optimization on a sequence, solved by a scan or by the dynamic programming of the longest increasing subsequence. More...

Files

file  ADS/optimization/SequenceProblems.h
 Problems of optimization on a sequence, solved by a scan or by the dynamic programming of the longest increasing subsequence [see Subsequences.h]:

Functions

template<class T>
T ADS::trappingRainWater (const std::vector< T > &h)
 the water held by the columns of heights \( h \), no walls outside
template<std::integral T>
std::vector< T > ADS::largestDivisibleSubset (std::vector< T > A)
 a largest subset of \( A \) in which of any two items one divides the other, sorted
template<class T>
T ADS::maxSumIncreasingSubsequence (const std::vector< T > &A)
 the largest sum of a strictly increasing subsequence of \( A \), 0 for the empty one
template<class T, class Less>
std::vector< std::size_t > ADS::longestChainSubsequence (const std::vector< T > &S, Less less)
 the places of a longest subsequence of \( S \) each item of which precedes the next by less, a strict partial order
template<class T, class Hash = std::hash< T >>
std::pair< std::size_t, std::size_t > ADS::longestDistinctSegment (const std::vector< T > &A)
 the start and the length of the first longest segment of \( A \) with no value repeated

Detailed description

Problems of optimization on a sequence, solved by a scan or by the dynamic programming of the longest increasing subsequence.

The definitions, the invariants and the costs of the area are in Theory of optimization.

Function documentation

◆ largestDivisibleSubset()

template<std::integral T>
std::vector< T > ADS::largestDivisibleSubset ( std::vector< T > A)

a largest subset of \( A \) in which of any two items one divides the other, sorted

In pseudocode (exercise B3 of 28/08/2026, Sottoinsieme divisibile, of the Esercizi Programmazione dinamica of the book), the longest chain of divisors on the items sorted:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{largestDivisibleSubset}( A )\quad \triangleright\ L[ i ] \text{ the longest chain of divisors ending at } A[ i ] \cr 2 & \quad \text{sort } A \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad ( L[ i ] , \mathit{pred}[ i ] ) \gets ( 1 , \textbf{nil} ) \cr 5 & \qquad \textbf{foreach}\ j < i \text{ with } A[ j ] \mid A[ i ] \text{ and } L[ i ] < L[ j ] + 1\ \textbf{do}\ ( L[ i ] , \mathit{pred}[ i ] ) \gets ( L[ j ] + 1 , j ) \cr 6 & \quad \textbf{return}\ \text{the chain of } \mathit{pred} \text{ back from the first largest } L[ i ] \text{, reversed} \end{array}} \]

Precondition
\( A \) positive and distinct
Postcondition
each item divides the next

◆ longestChainSubsequence()

template<class T, class Less>
std::vector< std::size_t > ADS::longestChainSubsequence ( const std::vector< T > & S,
Less less )

the places of a longest subsequence of \( S \) each item of which precedes the next by less, a strict partial order

In pseudocode (exercise B4 of 06/06/2011, Scatole, of the Esercizi Programmazione dinamica of the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestChainSubsequence}( S , \prec )\quad \triangleright\ \text{as } \mathrm{largestDivisibleSubset} \text{, with } \prec \cr 2 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad ( L[ j ] , \mathit{pred}[ j ] ) \gets ( 1 , \textbf{nil} ) \cr 4 & \qquad \textbf{foreach}\ i < j \text{ with } S[ i ] \prec S[ j ] \text{ and } L[ j ] < L[ i ] + 1\ \textbf{do}\ ( L[ j ] , \mathit{pred}[ j ] ) \gets ( L[ i ] + 1 , i ) \cr 5 & \quad \textbf{return}\ \text{the chain of } \mathit{pred} \text{ back from the first largest } L[ j ] \text{, reversed} \end{array}} \]

Postcondition
increasing places, less on each two consecutive items (audit)

◆ longestDistinctSegment()

template<class T, class Hash = std::hash< T >>
std::pair< std::size_t, std::size_t > ADS::longestDistinctSegment ( const std::vector< T > & A)

the start and the length of the first longest segment of \( A \) with no value repeated

In pseudocode (exercise A3 of 04/09/2020, Longest single, of the Esercizi Programmazione dinamica of the book), \( \mathit{last} \) a hash table of the last place of each value:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestDistinctSegment}( A[ 0 .. n - 1 ] )\quad \triangleright\ A[ s .. e ] \text{ the window, } \mathit{last} \text{ a table} \cr 2 & \quad ( s , b , \ell ) \gets ( 0 , 0 , 0 ) \cr 3 & \quad \textbf{for}\ e \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathit{last} \text{ holds } A[ e ] \text{ at a place } \ge s\ \textbf{then}\ s \gets \mathit{last}[ A[ e ] ] + 1\quad \triangleright\ \text{past the repeat} \cr 5 & \qquad \mathit{last}[ A[ e ] ] \gets e;\ \textbf{if}\ \ell < e + 1 - s\ \textbf{then}\ ( b , \ell ) \gets ( s , e + 1 - s ) \cr 6 & \quad \textbf{return}\ ( b , \ell ) \end{array}} \]

Postcondition
the values of the segment distinct (audit)

◆ maxSumIncreasingSubsequence()

template<class T>
T ADS::maxSumIncreasingSubsequence ( const std::vector< T > & A)

the largest sum of a strictly increasing subsequence of \( A \), 0 for the empty one

In pseudocode (Jacobson and Vo, beyond the book, for the exercise B3 of 16/06/2021, Massima somma crescente, of its Esercizi Programmazione dinamica), \( F \) a Fenwick tree of the prefix maxima of \( D \) on the ranks of the values:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumIncreasingSubsequence}( A ) \cr 2 & \quad \mathit{best} \gets 0;\ F \gets \text{all } 0 \cr 3 & \quad \textbf{foreach}\ a \in A\ \textbf{do} \cr 4 & \qquad r \gets \text{the rank of } a \text{ among the distinct values} \cr 5 & \qquad d \gets \max\{ a + \text{the maximum of } F \text{ over the } \text{ranks below } r , 0 \} \cr 6 & \qquad \mathit{best} \gets \max\{ \mathit{best} , d \};\ \text{raise } F \text{ at } r \text{ to } d \cr 7 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Postcondition
the result is not negative

◆ trappingRainWater()

template<class T>
T ADS::trappingRainWater ( const std::vector< T > & h)

the water held by the columns of heights \( h \), no walls outside

In pseudocode (exercise B2 of 20/07/2026, Acqua alta, of the Esercizi Programmazione dinamica of the book), \( \ell \) and \( r \) the highest columns met from the left and from the right:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{trappingRainWater}( h[ 0 .. n - 1 ] )\quad \triangleright\ \ell , r \text{ the highest bars met from each side} \cr 2 & \quad ( i , j , \ell , r , w ) \gets ( 0 , n - 1 , h[ 0 ] , h[ n - 1 ] , 0 ) \cr 3 & \quad \textbf{while}\ i < j\ \textbf{do}\quad \triangleright \ \text{the lower side bounds the water on its next bar} \cr 4 & \qquad \textbf{if}\ r \le \ell\ \textbf{then}\ j \gets j - 1;\ r \gets \max\{ r , h[ j ] \};\ w \gets w + r - h[ j ] \cr 5 & \qquad \textbf{else}\ i \gets i + 1;\ \ell \gets \max\{ \ell , h[ i ] \};\ w \gets w + \ell - h[ i ] \cr 6 & \quad \textbf{return}\ w \end{array}} \]

Postcondition
the result is not negative