Блог пользователя Alireza_Asadi

Автор Alireza_Asadi, история, 5 лет назад, По-английски

Hi Everyone! I have asked this question on stackexchange and got no chance getting an answer to my problem. Will you plz help me solve it?

I have come up with a solution but I don't exactly know if it is true. I think for the tallest possible height, we can always get to that by adding d to the highest tower. and for the lowest possible height, we can get the min of the array and add 1 to its m neighboring towers in a for loop and keep doing this again. finally the tallest tower will have the minimum height. Am I correct?

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

The above comment for max tower height should be correct. Take the maximum value and add d.

For min I think the answer is stuck between two cases. If d is > n/2 some elements might always be in part of an update operation. Think about how any update on n=3, m=2 will cover the middle element. So the cases are between if that element will be updated in all updates and if it wont. You first take the min of all of the elements that are not guaranteed to be in an update operation, since you can just do all d updates on some range that doesn't involve it. Then you take the min out of all of the elements that have to be in an update and add d to it. The answer should be the min of the two. Here's an example

n=3, m=2, d=1

[3, 1, 2]

so 3 and 2 are not guaranteed to be in an update so we have min(2, 3) = 2, then 1 is guaranteed to be in an update so we have min(2, 1+d = 1+1 = 2) = 2 so the answer would be 2. If arr[2] had been 0, the answer would have been 1.

I think this is correct, and I hope it helps.

  • »
    »
    5 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    I think your solution finds the 'shortest possible tower after d updates', but the question is (seemingly) asking for 'shortest possible tallest tower after d updates', i.e. if you do your updates optimally and find the tallest tower, how short can it be.

    You can see in the example they provide,

    input #1
    3 2 1
    4 3 5
    output #1
    6 5
    

    The shortest tower can be 4 (we can make [5,4,5]), but the answer is 5 because no matter how we do our updates, some tower will be at least 5 tall.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

Maximum: $$$max(h_i) + d$$$.

Minimum: Binary search for the answer.

Let's guess that the answer is $$$V$$$ or less. This means we have to be able to add 1 to an m-sized interval $$$d$$$ times, without any tower being taller than V. This is trivially not possible if we already have a tower taller than $$$V$$$; otherwise, for each tower of heigh $$$h_i$$$ we know how many 'addition intervals' it can be included in, $$$V-h_i$$$. Let's call these leftover values $$$a_i$$$.

Consider the first tower with value $$$a_1$$$. The only way to include this tower in an addition is to add 1 to $$$h_1, h_2, ..., h_m$$$, equivalent to subtracting from $$$a_1, ..., a_m$$$, which we can if $$$min(a_1, ..., a_m) \gt 0$$$. A necessary observation is that we can't go wrong by doing this.

Proof: let's assume instead of subtracting from $$$a_1,...,a_m$$$ we instead subtract from $$$a_k, ..., a_m, ..., a_{k+m}$$$ for some $$$k \leq m$$$, and that this is an optimal solution. Then we can 'undo' this operation, adding 1 to $$$[a_k, a_{k+m}]$$$, and apply it instead to $$$[a_1, a_m]$$$ (which we can, since we just added 1 to $$$a_k, ..., a_m$$$, so their min > 0).

So we count $$$operationsDone += min(a_1,...,a_m)$$$, and subtract this minimum from $$$a_1,...a_m$$$. We move on to the interval $$$[a_2,a_{m+1}]$$$ where the same logic applies again — find minimum of $$$[a_2,a_{m+1}]$$$, add it to operations done, subtract it from those elements, and move on, until we reach the end of the array.

If we manage to get $$$operationsDone \geq d$$$, the answer is $$$V$$$ or less (we managed to do at least $$$d$$$ operations without any tower being taller than $$$V$$$, otherwise it's $$$V+1$$$ or more. Binary search until we know exactly what the answer is.

To do the above operations quickly (find minimum and subtract value from range), you can use a lazy segment tree. The entire thing would then run in $$$O(nlogn \cdot log(d+h_i))$$$ (We binary search the answer between 0 and $$$d+max(h_i)$$$, and each pass takes $$$nlogn$$$ time to check).

There might be solutions where you can cut some logs away, but this would pass TL on a contest.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Binary search on lowest possible max height. We know the final sum heights d*m+h1+h2.....

We can check if we can achieve the sum while maintaining our maximum and increasing each number by atmost d.

Code
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Use ternary search and priority_queue to solve the minimum.