//! Unrestricted knapsack of Corollary `knapsack` (no windows, no constants): //! L_k = min_{A,B} 2(A+B) - Phi((A+1)(B+1) - k). //! Phi is computed once for all budgets up to Wmax. A step of weight j at //! any node needs total debit >= T(j), so only weights with T(j) <= Wmax //! matter, and a group of weight j needs only its floor(Wmax/j) largest //! profits, i.e. the floor(Wmax/j) largest unit savings overall. //! Used as ground truth for moderate k. use crate::dp::Table; use crate::polygon; use crate::solver::{count_of, n_min_for, rect, tri, KResult}; use crate::tree::Tree; pub fn solve_all(x: i64, polygons: bool) -> Vec { let n_hi_max = ((2.0 * std::f64::consts::PI * x as f64).sqrt()).ceil() as i64 + 1; let wmax = (1..=x).map(|k| count_of(n_hi_max) - k).max().unwrap().max(1); // enumerate enough nodes: at least wmax of them let mut z = 0.05; let tree = loop { let t = Tree::build(z); if t.n_global() as i64 >= wmax + 4 { break t; } z /= 2.0; }; let mut ids: Vec = (0..tree.n_global() as u32).collect(); ids.sort_by(|&a, &b| tree.sig(b as usize).partial_cmp(&tree.sig(a as usize)).unwrap().then(a.cmp(&b))); let mut jmax = 1i64; while tri(jmax + 1) <= wmax { jmax += 1; } let mut tot = 0.0; for j in 1..=jmax { let g = (wmax / j) as usize; tot += ids[..g].iter().map(|&i| tree.sig(i as usize)).sum::(); } let e = (tot + 1.0).log2().ceil() as i32; let scale = 2f64.powi((60 - e).clamp(20, 60)); let mut items: Vec> = vec![vec![]; (jmax + 1) as usize]; for j in 1..=jmax { let g = (wmax / j) as usize; items[j as usize] = ids[..g].iter().map(|&i| ((tree.sig(i as usize) * scale).round() as i64, i)).collect(); } let table = Table::build(&mut items, (wmax + 1) as usize, polygons); let sc = scale as i128; let mut out = vec![]; let mut cnt = vec![0i64; tree.n_global()]; for k in 1..=x { let n_lo = n_min_for(k); let n_hi = ((2.0 * std::f64::consts::PI * k as f64).sqrt()).ceil() as i64 + 1; let mut best: Option<(i128, i64)> = None; for n in n_lo..=n_hi { let w = count_of(n) - k; assert!(w <= wmax); let v = 2 * n as i128 * sc - table.value(w) as i128; if best.map_or(true, |b| v < b.0) { best = Some((v, n)); } } let (_, n) = best.unwrap(); let w = count_of(n) - k; let perim = (2 * n) as f64 - table.value(w) as f64 / scale; let (a, b) = rect(n); let mut r = KResult { k, m: 0, perimeter: perim, a, b, certified: true, delta_star: 0.0, n_feasible: 0, vertices: None, nverts: 0, boundary: 0, longest_edge: 0.0, longest_steps: 0, check_ok: None, check_msg: String::new(), }; if polygons { let mut touched = vec![]; table.traceback(w, |o| { cnt[o as usize] += 1; touched.push(o); }); let verts = polygon::vertices(&tree, a, b, &|g| cnt[g]); for o in touched { cnt[o as usize] = 0; } let ch = polygon::check(&verts); let ok = ch.convex && ch.count == k as i128 && (ch.perimeter - perim).abs() <= 1e-9 * (1.0 + perim); r.check_ok = Some(ok); r.check_msg = format!("convex={} pick_count={} perim_vertices={:.12}", ch.convex, ch.count, ch.perimeter); r.vertices = Some(verts); } out.push(r); } out }