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

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

Suppose we have an array which consist of n elements: a1, a2, a3, ..... , an and we have given the value of k which is the count of removed elements from the array. So after removing k elements from the array what is the maximum value of absolute difference of the adjacent elements of the array. Suppose,

n=5 k=3

1 2 5 2 1

then we remove 1,2,2 then the remaining elements of the array will be 5 1 so, ans=abs(5-1)=4 (maximum value which we can get).

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

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

what is constraint on ( n and k ) ? can you provide question link ?

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

For every i from 1 to n, choose the minimum and maximum in the range [i-k-1,i-1] (You can use a segment tree or c++ set or deque for the same) and find the absolute difference of both with a[i]. Now the answer will be the maximum among all such 2*n absolute values Using deque complexity will be just O(n)

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

You can build an array cointaining only the differences, let's call it v2.

On each of the k steps, you remove the minimum element from v2 ( you are actually removing one of the elements of the original array, v ) and update it's left or right side, depending on which element of v was the optimal removal choice.

Building time complexity of the segment tree is O(n), update and range minimum query time complexities are O(logn). Picking and changing one of the elements of v is O(1). Total time complexity is O(n + k*log^2 n).

I hope I'm not messing anything up, but I think that should be it.