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
# | User | Rating |
---|---|---|
1 | tourist | 3778 |
2 | Benq | 3592 |
3 | ecnerwala | 3521 |
4 | Um_nik | 3423 |
5 | jiangly | 3375 |
6 | Petr | 3342 |
7 | Radewoosh | 3337 |
8 | scott_wu | 3313 |
9 | maroonrk | 3265 |
10 | yosupo | 3259 |
# | User | Contrib. |
---|---|---|
1 | 1-gon | 205 |
2 | Errichto | 201 |
3 | rng_58 | 194 |
3 | SecondThread | 194 |
5 | awoo | 186 |
6 | vovuh | 183 |
7 | Um_nik | 182 |
8 | antontrygubO_o | 177 |
9 | Ashishgup | 175 |
10 | -is-this-fft- | 171 |
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
Name |
---|
Auto comment: topic has been updated by AdnanShamsi (previous revision, new revision, compare).
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.
in pdbs multiset upper_bound work as lower_bound vice-versa https://codeforces.com/blog/entry/11080?#comment-533322
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.
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
Thanks for explanation. Now I got 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.
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.
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.