Problem Link

Difficulty

Pre-requisites

Problem

Find the maximal score that is possible for Chef to obtain in the jumping competition

How to get 20 points

As usual, we won’t consider the solutions that doesn’t lead to the complete solution in any way.

The solution to a problem is based on the following dynamic programming idea. Let’s denote the maximal score that we can get by the time we stand at the ith hill by f*. Clearly, we need to calculate the value of f[N].

Let’s denote the rules for the calculation of f*. Clearly, f[1] will be equal to a[1] because we can’t jump on the first hill from any other hill. For i ≥ 2, we can find all the hills, from which we can jump to the ith hill. These hills’ numbers are all possible js such that j < i and p[j] < p*. Therefore,

f[1] = a[1]

f = min{f[j] + (h - h[j])2} + a*** for all j < i, p[j] < p*.

If you do the calculation of each f* naively, this results in the total complexity of O(N2).

How to get 100 points

The main idea remains the same as for the 20-point solution. Now we need to calculate f*s in a faster way.

It gives us a new idea. If we can somehow maintain the data structure, capable of answering two following types of queries fast enough, we solve the problem:

Modification: set x = a* and y = b*. This routine will be called exactly once for each i.

Query: get the minimal value of x * z + y** for all i such than L ≤ i ≤ R for the given L, R, and z.

Indeed. On one hand, after the new value of f* is calculated, we can do a modification at the position p* with the parameters x[p] = a = f + h*2** and y[p] = b = -2h**. And on the other hand, when we need to calculate the value of the new f*, it will be equal to min{y[j]+x[j]h} + h2 + a** for all js such that j < p*.

So now we can concentrate on the implementation of the data structure described above. This is actually a well known problem and it is called convex-hull trick. Here is the comprehensive and detailed tutorial on it. The variation you need is a fully dynamic variant.

The approach described by the link above enables us to have a data structure that maintains the following operations:

To add a linear function of the form ax+b to the set

Find min{ax+b} for the given x among all the functions that are present in the set

Both these operations can be done in amortized O(log N) time.

But this is still not what we want, because we need range queries. But we can create a segment tree. In each node of this segment tree we can store a dynamic convex-hull trick structure described right above.

In this case, the operations on out desired data structure will look like this:

Modification: add the sought line to all the nodes that contain the position of this line. Since this is a segment tree, there won’t be more than O(log N) such nodes. An addition to the single node’s data structure is O(log N). So, we get the modification complexity of O(log2 N).

Query: it works just in the standard segment tree. The initial query segment will be covered by O(log N) query nodes, and in each of them we can find the minimal value of all included functions in O(log N) too. So, we get the query complexity of O(log2 N).

Find min(a*. x + b*) for some x, and L < i < R .
Then when we are calculating f*, we need to consider min(a[j]. h* + b[j]) for 0 < j < p*,
and then set a[p*] = -2h and b[p*] = h*^2 + f*.
Now we are ready to calculate f[i+1]. So why do we need a Segment Tree now?

There is a mistake in the tutorial.
They defined x[p*] = a = f* + h*^2
and y[p*] = b = -2h*
Then it is written that f* = min{y[j]+x[j] . h*} + h*^2 + a* for all js such that j < p*.
It should be f* = min{y[j] . h*+x[j]} + h*^2 + a* for all js such that j < p*, or interchange the definitions of x and y.

The full dynamic variant of convex-hull trick can be solved using divide-and-conquer without any data structures.

In order to build the convex offline, use divide and conquer, and the sub-problem is to calculate dp transition from all points i from [L, mid] to [mid + 1, R], still this is not complete, we have to consider the p* < p[j] constraint, so we use a second divide-and-conquer in the sub-problem, then the questions changes to

calculate dp transition from all points i from [L, mid] and at the same time p* in [L2, mid2], to all points from [mid + 1, R] and p* in [mid2 + 1, R2], then we can build the convex offline for the first half, and do a binary search on the second half.

the testdata for the 3rd subtask was not so strong , i wrote some SQRT decomposition solution for the 3rd subtask https://www.codechef.com/viewsolution/8371300 but its very slow in worst case so i switched to convex hull trick