37# include <gtest/gtest.h>
52 constexpr long long INF64 = std::numeric_limits<long long>::max() / 4;
60 for (
size_t g = 0; g <= groups; ++g)
63 for (
size_t i = 0; i <= n; ++i)
70 for (
size_t g = 1; g <= groups; ++g)
71 for (
size_t i = g; i <= n; ++i)
72 for (
size_t k = g - 1;
k < i; ++
k)
74 if (dp[g - 1][
k] == INF64)
76 const long long cand = dp[g - 1][
k] + cost(
k, i);
86 const size_t n =
w.
size();
92 for (
size_t i = 0; i < n; ++i)
93 pref(i + 1) = pref[i] +
w[i];
97 for (
size_t i = 0; i <= n; ++i)
100 for (
size_t j = 0; j <= n; ++j)
105 for (
size_t len = 2; len <= n; ++len)
106 for (
size_t i = 0; i + len <= n; ++i)
108 const size_t j = i + len;
109 size_t best = std::numeric_limits<size_t>::max();
110 const size_t seg = pref[j] - pref[i];
111 for (
size_t k = i + 1;
k < j; ++
k)
113 const size_t cand = dp[i][
k] + dp[
k][j] + seg;
125 if (base.
size() == 0)
133 for (
size_t i = 1; i < base.
size(); ++i)
135 const size_t l = (i >
w) ? (i -
w) : 0;
136 long long best = INF64;
137 for (
size_t j =
l; j < i; ++j)
140 dp(i) = best + base[i];
143 return dp[base.
size() - 1];
150 long long best = INF64;
151 for (
size_t j = 0; j < xs.
size(); ++j)
153 const long long d = xs[i] - xs[j];
154 const long long cand = d * d + ws[j];
163TEST(DPOptimizations, DivideConquerPartitionMatchesBruteForce)
165 std::mt19937
rng(20260226);
167 for (
size_t trial = 0; trial < 120; ++trial)
169 const size_t n = 1 + (
rng() % 36);
170 const size_t groups = 1 + (
rng() % std::min<size_t>(6, n));
174 for (
size_t i = 0; i < n; ++i)
175 a.
append(1 +
static_cast<long long>(
rng() % 11));
179 for (
size_t i = 0; i < n; ++i)
180 pref(i + 1) = pref[i] + a[i];
182 const auto cost = [&] (
const size_t k,
const size_t i) ->
long long
184 const long long d = pref[i] - pref[
k];
188 const auto fast = divide_and_conquer_partition_dp<long long>(
189 groups, n, cost, INF64
191 const auto slow = brute_partition_dp(groups, n, cost);
193 EXPECT_EQ(fast.optimal_cost, slow[groups][n]);
195 for (
size_t g = 1; g <= groups; ++g)
196 for (
size_t i = g + 1; i <= n; ++i)
197 EXPECT_LE(fast.split[g][i - 1], fast.split[g][i]);
201TEST(DPOptimizations, DivideConquerPartitionEdgeCases)
203 const auto cost = [] (
const size_t k,
const size_t i) ->
long long
205 const long long d =
static_cast<long long>(i -
k);
209 const auto r0 = divide_and_conquer_partition_dp<long long>(0, 0, cost, INF64);
210 EXPECT_EQ(r0.optimal_cost, 0);
212 const auto r1 = divide_and_conquer_partition_dp<long long>(0, 5, cost, INF64);
213 EXPECT_EQ(r1.optimal_cost, INF64);
215 const auto r2 = divide_and_conquer_partition_dp<long long>(7, 3, cost, INF64);
216 EXPECT_EQ(r2.optimal_cost, INF64);
219TEST(DPOptimizations, KnuthOptimalMergeMatchesBruteForce)
221 std::mt19937
rng(20260226 + 7);
223 for (
size_t trial = 0; trial < 150; ++trial)
225 const size_t n =
rng() % 35;
228 for (
size_t i = 0; i < n; ++i)
229 w.append(
static_cast<size_t>(
rng() % 25));
232 const size_t slow = brute_optimal_merge(
w);
233 EXPECT_EQ(fast.optimal_cost, slow);
236 for (
size_t len = 3; len <= n; ++len)
237 for (
size_t i = 0; i + len <= n; ++i)
239 const size_t j = i + len;
240 EXPECT_LE(fast.opt[i][j - 1], fast.opt[i][j]);
241 EXPECT_LE(fast.opt[i][j], fast.opt[i + 1][j]);
246TEST(DPOptimizations, KnuthOptimalMergeEdgeCases)
250 EXPECT_EQ(e.optimal_cost, 0u);
254 EXPECT_EQ(o.optimal_cost, 0u);
257TEST(DPOptimizations, ConvexHullTrickMatchesBruteForce)
265 for (
size_t i = 0; i < slopes.
size(); ++i)
267 cht.
add_line(slopes[i], intercepts[i]);
271 for (
long long x = -30; x <= 30; ++x)
273 long long best = INF64;
274 for (
size_t i = 0; i < lines.
size(); ++i)
276 const long long v = lines[i].value_at(x);
280 EXPECT_EQ(cht.
query(x), best);
284 for (
long long x = -30; x <= 30; ++x)
286 long long best = INF64;
287 for (
size_t i = 0; i < lines.
size(); ++i)
289 const long long v = lines[i].value_at(x);
297TEST(DPOptimizations, ConvexHullTrickSlopeChecksAndDuplicates)
301 EXPECT_THROW(cht.
query(0), std::runtime_error);
305 EXPECT_EQ(cht.
size(), 1u);
308 EXPECT_EQ(cht.
size(), 1u);
309 EXPECT_EQ(cht.
query(10), 13);
311 EXPECT_THROW(cht.
add_line(2, 0), std::domain_error);
314TEST(DPOptimizations, LiChaoMatchesBruteForce)
319 std::mt19937
rng(20260226 + 17);
320 for (
size_t i = 0; i < 80; ++i)
322 const long long m =
static_cast<long long>(
rng() % 51) - 25;
323 const long long b =
static_cast<long long>(
rng() % 401) - 200;
328 for (
long long x = -200; x <= 200; x += 3)
330 long long best = INF64;
331 for (
size_t i = 0; i < lines.
size(); ++i)
333 const long long cand = lines[i].value_at(x);
337 EXPECT_EQ(lc.
query(x), best);
341TEST(DPOptimizations, LiChaoDomainAndEmptyChecks)
346 EXPECT_THROW(lc.
query(0), std::runtime_error);
349 EXPECT_THROW(lc.
query(11), std::out_of_range);
352TEST(DPOptimizations, MonotoneQueueDPMatchesBruteForce)
354 std::mt19937
rng(20260226 + 31);
356 for (
size_t trial = 0; trial < 220; ++trial)
358 const size_t n = 1 + (
rng() % 200);
359 const size_t w = 1 + (
rng() % 20);
363 for (
size_t i = 0; i < n; ++i)
364 base.
append(1 +
static_cast<long long>(
rng() % 50));
366 const auto fast = monotone_queue_min_dp<long long>(base,
w);
367 const long long slow = brute_windowed_dp(base,
w);
369 EXPECT_EQ(fast.dp[base.
size() - 1], slow);
371 for (
size_t i = 1; i < n; ++i)
373 EXPECT_LT(fast.parent[i], i);
374 EXPECT_LE(i - fast.parent[i],
w);
379TEST(DPOptimizations, MonotoneQueueDPEdgeCases)
382 auto re = monotone_queue_min_dp<long long>(empty, 5);
383 EXPECT_EQ(re.dp.size(), 0u);
386 auto r1 = monotone_queue_min_dp<long long>(one, 0);
387 EXPECT_EQ(r1.dp.size(), 1u);
388 EXPECT_EQ(r1.dp[0], 17);
391 EXPECT_THROW(monotone_queue_min_dp<long long>(two, 0), std::domain_error);
394TEST(DPOptimizations, WeightedSquaredDistanceMatchesBruteForce)
400 ASSERT_EQ(got.size(), xs.
size());
402 for (
size_t i = 0; i < xs.
size(); ++i)
403 EXPECT_EQ(got[i], brute_weighted_sq_dist(xs, ws, i));
406TEST(DPOptimizations, WeightedSquaredDistanceRandomVsBrute)
408 std::mt19937
rng(20260226 + 101);
410 for (
size_t trial = 0; trial < 120; ++trial)
412 const size_t n = 1 + (
rng() % 60);
419 for (
size_t i = 0; i < n; ++i)
421 xs.
append(
static_cast<long long>(
rng() % 101) - 50);
422 ws.
append(
static_cast<long long>(
rng() % 80));
426 for (
size_t i = 0; i < n; ++i)
427 EXPECT_EQ(got[i], brute_weighted_sq_dist(xs, ws, i));
431TEST(DPOptimizations, WeightedSquaredDistanceValidatesSizes)
438TEST(DPOptimizations, DivideConquerPartitionPerf)
440 if (std::getenv(
"RUN_PERF_TESTS") ==
nullptr)
441 GTEST_SKIP() <<
"Skipping performance test; set RUN_PERF_TESTS=1 to enable";
445 const size_t groups = 100;
446 const size_t n = 2000;
449 auto cost = [](
size_t i,
size_t j) ->
long long
451 const long long diff =
static_cast<long long>(j - i);
455 const std::clock_t start_clock = std::clock();
456 const auto res = divide_and_conquer_partition_dp<long long>(groups, n, cost);
457 const std::clock_t end_clock = std::clock();
459 const double cpu_elapsed_ms = (
static_cast<double>(end_clock - start_clock) * 1000.0) / CLOCKS_PER_SEC;
462 EXPECT_LT(cpu_elapsed_ms, 500.0)
463 <<
"Performance regression: divide_and_conquer_partition_dp took " << cpu_elapsed_ms <<
"ms CPU time";
468 EXPECT_EQ(res.optimal_cost, cost(0, n) / groups);
Classical DP optimization toolkit: D&C DP, Knuth, CHT, Li Chao, and monotone-queue transitions.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Convex Hull Trick for minimum queries.
T query_monotone(const T x)
Query minimum with non-decreasing x (amortized O(1)).
void reset_query_cursor() noexcept
T query(const T x) const
Query minimum value at arbitrary x (O(log n)).
size_t size() const noexcept
void add_line(const T slope, const T intercept)
Insert a new line; slopes must be non-increasing.
Li Chao tree for min line queries on an integral x-domain.
void add_line(const T slope, const T intercept)
Main namespace for Aleph-w library functions.
Array< T > min_weighted_squared_distance_1d(const Array< T > &xs, const Array< T > &weights)
Weighted squared-distance lower envelope on a line.
Knuth_Optimization_Result< size_t > optimal_merge_knuth(const Array< size_t > &weights)
Optimal adjacent merge cost via Knuth optimization.
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)