×

# Minimum Maximum Partition

Problem Setter: Istasis Mishra

Problem Tester: Istasis Mishra

Editorialist: Taranpreet Singh

## Problem:

Given two arrays P and Q of Size N and an integer K, Minimize AND maximize the cost of partitioning the range [1...N] into exactly K partitions, where cost of a partition is defined as $min(P[L...N])*max(Q[1...R])$.

## Prerequisites:

Dynamic Programming - Convex Hull Optimization or Divide and Conquer Optimization(But don't worry, its not as hard as it sounds)

## Solution:

Since We have cost of a partition defined as $min_{L \leq i \leq N}(P[i]) \times max_{1 \leq i \leq R}(Q[i])$, we can observe that min(P[L....N]) = min(P[L], P[L+1....N]) and max(Q[1...R]) = max(Q[1...L-1], Q[L]). This means that we can pre-calculate min(P[i..N]) as well as max(Q[1...i]) for 0<=i<=N-1.

Now, The most complicated part of this problem is that, both P and Q can also have negative values, implying that in case both min(P[L..N) and max(Q[1..R]) are negative, we may still have positive cost, thus, have to be considered for Maximum solution, otherwise if there were only positive values then there is a nice greedy solution to this.

Now, to get the correct solution that receives 36 points, there is a recurrence. Let us define a function $F$ such that $F(i, k) = \text{Making k partitions till i}$. Defining the states like this gives the clear recurrence that $F(i, k) = min_{1 \leq j \leq i}(F(j, k-1) + A[j] \times B[i])$ where A[j] is the pre-computed value of min of all P from j to N and B[i] is the max of all Q from i to N. Using this as DP, we see that we have to traverse for each i, j and k and this is an $O(N^2 \times K)$ solution and gives 36 points.

We need to optimize it, and for that, it would require us the knowledge of Convex Hull Optimization. As we can see the Dp is of the form $y = m \times x + c$.

Basically, Convex hull deals with a set of lines with slope m and y-intercept, arranged on basis of slope in a stack, Speeding up selection of j in above recurrence to O(logN) time by use of binary search, giving us a total runtime of O(N^2*logN) which will easily pass the Time Limit.

The dp optimization mentioned above is usually used once, but in our problem, we are going to maintain K hulls one for each layer of DP. As you can see, if we treat the DP as having K layers then each layer depends on the previous layer. Thus, we can see that for each k we have to calculate the answer from k-1 and then add all the calculated “lines” in the current iteration to the stack.

Also, we can see that for min we require the “slope” to be decreasing and to find the min we want the slope to be increasing. Even though, this problem can be solved using a dynamic convex hull, this could be overcome by changing the DP to be calculated from N to 1 instead of 1 to N and also changing what represents the slope and the $x$. For more details, refer to the setters implementation below.

Now, to incorporate negative values and also accommodate for equal slopes, changes are required to be made in bad function (Refer code of Acquire problem here.

Also, one of the user solved this problem using divide and Conquer optimization. Here's the link to submission.

PS:Sorry for delay, its kinda hard writing editorial for a problem you yourself haven't solved yet.

Setter's solution

C++ Implementation

Java Implementation

This question is marked "community wiki".

2.7k827
accept rate: 22%

2.0k318

 1 Divide and conquer optimization is simply not applicable for this problem. The required property for the optimization does not hold. It is not difficult to come up with a test case where it fails (link), so I guess the test cases are pretty weak. Also, this is not crucial, but the binary search is not necessary when using convex hull optimization since the query parameter $x$ only monotonically increases or decreases. It is possible to do it in $\mathcal{O}(NK)$. Here's my solution that works with the mentioned complexity: link. answered 06 Jan, 20:07 6★meooow ♦ 5.8k●4●13 accept rate: 47% Can you please explain your solution ? ☺️ (11 Jan, 17:57)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags: