Problem Description
Problem Analysis
Chinese statement:
Xiaobai is playing a game. There are n dominoes arranged in a row on the table. He has k chances; each time he can choose a domino that has not fallen yet and push it to the left or right. When each domino falls, if it touches a domino that has not fallen, it can knock it down. Xiaobai can now set the heights of the dominoes arbitrarily, but the heights are integers and at least 1, and Xiaobai wants to minimize the sum of all domino heights under the premise that all dominoes can be knocked down.
Official solution:
First, for dominoes we only need to consider pushing them all to the right; second, the prerequisite for being able to knock down a domino is that the height is greater than or equal to distance + 1.
So if we push once, then the domino height = distance to the next domino + 1. Set the distance to the left of the first domino to infinity; if we can push nk times, then we just need to find the nk dominoes with the largest left-side distances and push them to the right; therefore we only need to sort and find the largest nk-1 distances.
There is a small trick: the number of pushes may be greater than the number of dominoes. Complexity O(nlogn)
From the statement, we can see that we should waste the largest distances as much as possible
So accumulate (distance + 1), then subtract the largest distances as much as possible
and we can obtain the minimum height
PS: Note the data range: int cannot store sum
| |