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

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

Given an array of integers H representing heights of towers and a positive integer B. It is allowed to either increase or decrease the height of every tower by B (only once).

How to calculate the minimum possible difference between the heights of the longest and the shortest tower after modifications.

Note: It is mandatory to either increase or decrease the height of every tower.

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

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

Auto comment: topic has been updated by Aafat_Wapas (previous revision, new revision, compare).

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

Decrease all towers height by B. Then you need to increase some subset of them by 2*B. If you sort them it will be some prefix.

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

Sort the heights and try every possible split into two halves [0, i-1] and [i, n-1], where you add B to the left half and subtract B from the right half. Then the difference for that split is max(a[n-1] - B, a[i-1] + B) - min(a[0] + B, a[i] - B).

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

    Can you please explain how you arrived at this approach.

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится +6 Проголосовать: не нравится

      I'm not sure, mostly intuition. But maybe this can help:

      So we have to add B to some elements $$$a_{i_1}...a_{i_k}$$$ and subtract B from the rest $$$a_{j_1}...a_{j_l}$$$. Let's say these sets are sorted, so the minimum of the first is $$$a_{i_1} + B$$$. The minimum of the other set is $$$a_{j_1} - B$$$. The overall minimum will then be $$$min(a_{i_1} + B, a_{j_1} - B)$$$.

      But if $$$a_{j_1} < a_{i_k}$$$ (i.e. they are not a split into halves of the sorted array), then we could exchange $$$a_{i_k}$$$ and $$$a_{j_1}$$$ and achieve a larger minimum.

      Same can be argued for maximum.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    With this,there is chance that we are making height of tower negative, which is not ideal

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

what does K do

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

Auto comment: topic has been updated by Aafat_Wapas (previous revision, new revision, compare).

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

first find minimum and maximum element of the array and find the mid = (min + max)/2

decrease all height more than mid by B and increase all height more than mid by B.

sort the array.

the diff between min and max of new array will be the answer.
we also have to check for answer in the original array.

»
3 года назад, # |
Rev. 6   Проголосовать: нравится +4 Проголосовать: не нравится

What is wrong in my approach

every state of dp will store a pair

dp(i,0) will store that using arr[i]-k and what will be the (min,max) from index 0 to i

dp(i,1) will store that using arr[i]+k and what will be the (min,max) from index 0 to i

ans=min(dp(n-1)(0).second-dp(n-1)(0).first,dp(n-1)(1).second-dp(n-1)(1).first)

Transition in state

temp1=min(dp[i-1][1].first,arr[i]-k) //dp[i-1][1].first will store the minimum vale till i-1 index considering arr[i]-k

temp2=max(dp[i-1][1].second,arr[i]-k)

temp3=min(dp[i-1][0].first,arr[i]-k)

temp4=max(dp[i-1][0].second,arr[i]-k)

if(abs(temp1-temp2)>abs(temp3-temp4)) dp[i][0]={temp3,temp4}

else dp[i][0]={temp1,temp2}

//same way we can find dp[i][1]