AdnanShamsi's blog

By AdnanShamsi, history, 4 years ago, In English

Sliding Cost

https://cses.fi/problemset/task/1077

Can someone explain how to approach this problem.

I was trying to do it using pdbs and fenwick tree but it showed me tle on last 4 test cases
https://pastebin.com/3qBy2WBW

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why you erase the upper_bound() here? I would have expected to erase to lower_bound().

ms.erase(ms.ub(arr[head - k]));

But maybe I did understand the code wrong.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi, did you solve this problem, I'm also stuck on this. Saw few solutions on github having Fenwick tree in them, but couldn't the idea.

  • »
    »
    4 years ago, # ^ |
    Rev. 4   Vote: I like it +1 Vote: I do not like it

    there is a better solution to this which i couldnt understand so i did a straightword solution which gave me AC.

    i have a pbds — which contains all elements of current window — now i can get the median of the currennt window in log(n)

    now 2 FWTs I used for —

    1) count of certain element

    2) sum

    so for e.g. if i have a window 2 2 3, fwt1[2] = 2 and fwt1[3] = 1 and fwt2[2] = 4 and fwt2[3] = 3

    now all i need is for each window is — elements above it, sum of elements above it, elements below it, sum of elements below it.

    since number range is large i used cordinate compression.

    look at the 7 lines i marked in my code and hopefully you will understand more

    CODE
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

We can do this by using the prefix sum array and median of the subarray of size k of the given array . As median is the closest point to all the numbers in the subarray of k size.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Using prefix sum array won't work in regard with computing the cost of converting each number to the median in a subarray, right? Because you can not say the cost is simply subarray's sum — subarray's size * its median value.

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Here's my implementation with BIT. First tree is for storing the cumulative frequency and second one is for storing elements in the current window.

code
»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Sorry for necroposting, but this problem can be solved by using multiset only ..
maintain two multiset, first one will contain elements smaller (k + 1) / 2 elements while the second multiset will contain largest (k / 2) elements of a subarray of size k.

This problem is the advanced version of this problem Link with also delete operations.

Code
»
16 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Here is my solution : ) Tags of my solution 1) BIT tree 2) Two Pointers 3) Compressing 4) STL

Code :https://ideone.com/BUXxJO

»
11 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
Easily Code with priority queue