31 CostFunc cost_func,
int size,
DPPoint* points) {
32 if (size <= 0 || max_step < min_step || min_step >= size)
39 for (
int i = 0; i < size; ++i) {
40 for (
int offset = min_step; offset <= max_step; ++offset) {
41 DPPoint* prev = offset <= i ? points + i - offset : NULL;
42 inT64 new_cost = (points[i].*cost_func)(prev);
43 if (points[i].best_prev_ != NULL && offset > min_step * 2 &&
44 new_cost > points[i].total_cost_)
47 points[i].total_cost_ += points[i].local_cost_;
49 tprintf(
"At point %d, local cost=%d, total_cost=%d, steps=%d\n",
50 i, points[i].local_cost_, points[i].total_cost_,
51 points[i].total_steps_);
55 int best_cost = points[size - 1].total_cost_;
56 int best_end = size - 1;
57 for (
int end = best_end - 1; end >= size - min_step; --end) {
58 int cost = points[end].total_cost_;
59 if (cost < best_cost) {
64 return points + best_end;
69 if (prev == NULL || prev ==
this) {
70 UpdateIfBetter(0, 1, NULL, 0, 0, 0);
74 int delta =
this - prev;
75 inT32 n = prev->n_ + 1;
76 inT32 sig_x = prev->sig_x_ + delta;
77 inT64 sig_xsq = prev->sig_xsq_ + delta * delta;
78 inT64 cost = (sig_xsq - sig_x * sig_x / n) / n;
79 cost += prev->total_cost_;
80 UpdateIfBetter(cost, prev->total_steps_ + 1, prev, n, sig_x, sig_xsq);
87 if (cost < total_cost_) {
inT64 CostWithVariance(const DPPoint *prev)
static DPPoint * Solve(int min_step, int max_step, bool debug, CostFunc cost_func, int size, DPPoint *points)