Problems of optimization on a sequence, solved by a scan or by the dynamic programming of the longest increasing subsequence [see Subsequences.h]:
More...
#include "ADS/Contract.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <functional>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ADS/optimization/impl/SequenceProblems.tpp"
Go to the source code of this file.
|
| 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
|
Problems of optimization on a sequence, solved by a scan or by the dynamic programming of the longest increasing subsequence [see Subsequences.h]:
- trappingRainWater(): the water held by columns of heights \( h_i \) after the rain, \( \sum_i \min( \max_{j \le i} h_j , \max_{j \ge i} h_j ) - h_i \); two indices from the ends, the lower side moving inwards, since its level is fixed by its own maximum: \( \Theta( n ) \) time and \( O( 1 ) \) space, instead of the two arrays of the maxima or the stack of the decreasing columns;
- largestDivisibleSubset(): the largest subset of distinct positive integers in which of any two one divides the other: on the items sorted, a chain in the order of divisibility, \( L[ i ] = 1 + \max\{ L[ j ] : j < i , a_j \mid a_i \} \), right by the transitivity of divisibility; \( \Theta( n^2 ) \) time;
- maxSumIncreasingSubsequence(): the largest sum of a strictly increasing subsequence, the heaviest increasing subsequence with the items as weights: \( D[ i ] = a_i + \max\{ D[ j ] : j < i , a_j < a_i \} \), the maximum over the smaller values from a Fenwick tree of the prefix maxima on the ranks, \( O( n \log n ) \) time instead of the \( \Theta( n^2 ) \) scan;
- longestChainSubsequence(): the longest subsequence of \( S \) each item of which precedes the next in a strict partial order, e.g., boxes each fitting in the next, \( L[ j ] = 1 + \max\{ L[ i ] : i < j , S_i \prec S_j \} \), \( \Theta( n^2 ) \) comparisons; for a total order it is the longest increasing subsequence, in \( O( n \log n ) \). In \( d \) dimensions, the boxes free to be sorted and rotated give the box stacking problem, the longest path in the DAG of the dominance;
- longestDistinctSegment(): the longest segment of \( A \) with no value repeated, by a window whose left end jumps past the last occurrence of a repeated value, with a hash table of the last occurrences; \( \Theta( n ) \) expected time.
The contracts [see Contract.h]: the items of largestDivisibleSubset() positive and distinct, and the subset divisible; the subsequences found increasing in the given order. The functions are defined in SequenceProblems.tpp.
- See also
- A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13 and the Esercizi Programmazione dinamica, exercises B4 of 06/06/2011, A3 of 04/09/2020, B3 of 16/06/2021, B2 of 20/07/2026 and B3 of 28/08/2026, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
-
G. Jacobson, K.-P. Vo, Heaviest increasing/common subsequence problems, Proc. 3rd Symposium on Combinatorial Pattern Matching, LNCS 644, 52-66, 1992, doi:10.1007/3-540-56024-6_5
-
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
- Author
- Donato Meoli
Dipartimento di Informatica
Università di Pisa
- Copyright
- © by Donato Meoli