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

Автор AdnanShamsi, история, 4 года назад, По-английски

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

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

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

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # ^ |
    Rev. 4   Проголосовать: нравится +1 Проголосовать: не нравится

    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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

Code :https://ideone.com/BUXxJO

»
11 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
Easily Code with priority queue