//! SMAWK row maxima (Aggarwal, Klawe, Moran, Shor, Wilber). //! //! Computes, for every row, the *leftmost* column attaining the row maximum //! of an implicitly given matrix that is totally monotone in the strict //! sense: for rows r < r' and columns c < c', //! f(r,c) < f(r,c') implies f(r',c) < f(r',c'). //! Inverse-Monge matrices satisfy this, and so do inverse-Monge matrices //! whose entries above the diagonal (c > r) are replaced by a common very //! negative constant, which is how we use it. pub fn row_maxima i64>(nrows: usize, ncols: usize, f: &F, out: &mut Vec) { out.clear(); out.resize(nrows, 0); if nrows == 0 || ncols == 0 { return; } let rows: Vec = (0..nrows as u32).collect(); let cols: Vec = (0..ncols as u32).collect(); rec(&rows, &cols, f, out); } fn rec i64>(rows: &[u32], cols: &[u32], f: &F, out: &mut [u32]) { if rows.is_empty() { return; } // REDUCE: keep at most |rows| columns that can still hold a leftmost max. let mut st: Vec = Vec::with_capacity(rows.len().min(cols.len())); for &c in cols { while let Some(&top) = st.last() { let r = rows[st.len() - 1] as usize; if f(r, top as usize) < f(r, c as usize) { st.pop(); } else { break; } } if st.len() < rows.len() { st.push(c); } } // Recurse on the odd rows. if rows.len() > 1 { let odd: Vec = rows.iter().skip(1).step_by(2).copied().collect(); rec(&odd, &st, f, out); } // Interpolate the even rows. let mut k = 0usize; let mut i = 0usize; while i < rows.len() { let r = rows[i] as usize; let stop = if i + 1 < rows.len() { out[rows[i + 1] as usize] } else { *st.last().unwrap() }; let mut best = st[k]; let mut bv = f(r, best as usize); while st[k] != stop { k += 1; let v = f(r, st[k] as usize); if v > bv { bv = v; best = st[k]; } } out[r] = best; i += 2; } } /// The same leftmost row maxima by plain divide and conquer: solve the /// middle row by a scan, then recurse on the rows above and below with the /// column range split at its argmax (monotone by total monotonicity). /// O((rows + cols) log rows) evaluations; used only for comparison. pub fn row_maxima_dc i64>(nrows: usize, ncols: usize, f: &F, out: &mut Vec) { out.clear(); out.resize(nrows, 0); if nrows > 0 && ncols > 0 { dc(0, nrows, 0, ncols, f, out); } } fn dc i64>(rlo: usize, rhi: usize, clo: usize, chi: usize, f: &F, out: &mut [u32]) { if rlo >= rhi { return; } let mid = (rlo + rhi) / 2; let mut best = clo; let mut bv = f(mid, clo); for c in clo + 1..chi { let v = f(mid, c); if v > bv { bv = v; best = c; } } out[mid] = best as u32; dc(rlo, mid, clo, best + 1, f, out); dc(mid + 1, rhi, best, chi, f, out); } #[cfg(test)] mod tests { use super::*; struct Lcg(u64); impl Lcg { fn next(&mut self) -> u64 { self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); self.0 >> 33 } } #[test] fn concave_convolution_matches_brute_force() { let mut rng = Lcg(12345); const NEG: i64 = i64::MIN / 4; for _ in 0..3000 { let n = 1 + (rng.next() % 40) as usize; let g = (rng.next() % 12) as usize; // nondecreasing F (as in the knapsack) or arbitrary F let mono = rng.next() % 2 == 0; let mut fcol = vec![0i64; n]; let mut acc = 0i64; for x in 0..n { if mono { acc += (rng.next() % 5) as i64; fcol[x] = acc; } else { fcol[x] = (rng.next() % 30) as i64; } } // concave R: sorted decreasing profits (with ties) let mut p: Vec = (0..g).map(|_| (rng.next() % 6) as i64).collect(); p.sort_unstable_by(|a, b| b.cmp(a)); let mut r = vec![0i64; g + 1]; for t in 0..g { r[t + 1] = r[t] + p[t]; } let f = |y: usize, x: usize| -> i64 { if x > y { NEG } else { fcol[x] + r[(y - x).min(g)] } }; let mut out = vec![]; row_maxima(n, n, &f, &mut out); let mut out2 = vec![]; row_maxima_dc(n, n, &f, &mut out2); assert_eq!(out, out2, "dc differs"); for y in 0..n { let mut best = NEG; let mut arg = 0; for x in 0..n { if f(y, x) > best { best = f(y, x); arg = x; } } assert_eq!(f(y, out[y] as usize), best, "value mismatch"); assert_eq!(out[y] as usize, arg, "not leftmost"); } } } }