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

Автор alaincr7, история, 7 месяцев назад, По-английски

Given an array arr of N integers. Each array has some hardness

hardness = max abs(arr[i+1]-arr[i]) for all 1<=i<=N-1

And hardness = 0 if N<=1

You are given a number K you can change at most K elements of the array. You have to minimize the hardness of the array

Both N and K were less than 2000

where arr[i]>=-1e9 and arr[i]<=1e9

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

»
7 месяцев назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

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

»
7 месяцев назад, скрыть # |
 
Проголосовать: нравится -13 Проголосовать: не нравится

This should be easy one.

»
7 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится -10 Проголосовать: не нравится

if the array is {a1,a2,a3, ... an}, the answer is always an-a1 because the rest of the elements get canceled out, so just decrease an, and increase a1 as much as possible

»
7 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится -10 Проголосовать: не нравится

[REDACTED]

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

Lets say you have fixed $$$x$$$, consider $$$dp_i,_j$$$ which means the minimum number changes needed to have this property: The hardness of prefix $$$i$$$ is smaller than $$$x$$$, and last $$$j$$$ elements were changed.

If all elements of some segment $$$[l, r]$$$ were changed, then u can make the hardness of the segment $$$[l-1, r+1]$$$ equal to $$$\lceil \frac{a_{r+1}-a_{l-1}}{r-l+3}\rceil$$$(I am not sure if this formula is correct)

Let segment $$$[l, r]$$$ be good if and only if $$$\lceil \frac{a_{r+1}-a_{l-1}}{r-l+3}\rceil \leq x$$$

You have these transitions:

$$$1.$$$ if $$$[i, i - j + 1]$$$ is good then: $$$dp_i,_j=min(dp_i,_j, dp_{i-1},_{j-1}+1)$$$

$$$2.$$$ $$$dp_i,_0=min(dp_i,_0, dp_{i-1},_{j-1})$$$

Now u can just binsearch the value $$$x$$$, and this should work in $$$O(n^2\log x)$$$

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

    Is there a n log(x) , binary search solution for this

    I was thinking for each X in binary search , we check greedily if its possible to make diff <= X by using at most K operations.

    // checker greedy if(abs(arr[i] — arr[i-1]) > X // then K has to be reduced .

    Now which leads to maximum distances without using another K-1 --> changing arr[i] or arr[i-1]

    Oh shit , will not work : changing arr[i-1] will make me recompute everything till here.

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

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