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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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 |
---|
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.net/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.
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.
void solve() { int n , k; cin >> n >> k;
}
Here is my solution : ) Tags of my solution 1) BIT tree 2) Two Pointers 3) Compressing 4) STL
Code :https://ideone.com/BUXxJO