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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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://mirror.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.
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