Problem: You are given an aray of N elements(N<1e6). You have to find maximal average of some continuous part of array. Minumum length of part=A, Maximum length =B (A,B<N).
Example: A=2,B=4,array={1,-3,6,2,5} Answer: 4.333
How to solve this problem?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Problem: You are given an aray of N elements(N<1e6). You have to find maximal average of some continuous part of array. Minumum length of part=A, Maximum length =B (A,B<N).
Example: A=2,B=4,array={1,-3,6,2,5} Answer: 4.333
How to solve this problem?
Название |
---|
Use binary search on answer. We want to know if the answer is more than x. Subtract x from all elements. Now we want to find a segment with length from A to B and a positive sum. Can you do it?
Actually, it took me a moment thinking about what you said this is a good idea for the check method in the binary search. If I got it right when you find a segment of length From A to B with positive-sum this means that we can obtain a better answer.
Also, khadaev do you think we can optimize the part where we look for the segment of length from A to B using prefix sum (calculated once in the beginning)? Or do you have any idea about it?
We need to find max(pi + a, ..., pi + b) for all i where pi are prefix sums. Easy way: use segment tree. Faster way: use queue with a maximum.
great, thank you :D
Binary search on answer. You will need to check if average of any subarray >= A. Then just subtract A from each value of the array and the problem reduces to — check if sum of any subarray >= 0. That can be done using DP. Time complexity is O(N*logA). There might be better solutions than this one.
Run a binary search for an answer. So, now the problem is how to check is there a subarray of a length from that interval with average no less than x. Using the array of the partial sums si = a1 + a2 + ... + ai, s0 = 0, average of interval (l, r) is equal to . So you have to check is there a pair 0 ≤ l < r ≤ n such that . Then sr - sl ≥ x * (r - l). sr - x * r ≥ sl - x * l. Denote ti = si - x * i, then our condition turns to tr ≥ tl. Iterate through r and then you need to check is there a l: r - B ≤ l ≤ r - A that tl ≤ tr. So you have to take the minimum from the segment (r - B, r - A) from the array t. It can be done with the segment tree(not sure it will pass the time limit) or you can use the special queue which allows to add and remove elements and to take a maximum. Last one will work in O(n * log(ANS * eps))
Thanks. After thinking about using binary search its not hard. Checking can be done with multiset in O(nlog n).
Binary search in the answer isn't needed. Consider taking partial sums si = a1 + ... + ai, with s0 = 0. Then the average of segment (i, j] is equal to . Look now for every index i the point pi = (i, si). Now the average of a segment [i, j) is the slope of points pi and pj, if we fix the right point j, we need to look for the point pi with A ≤ j - i ≤ B that maximize the slope with pj. Isn't difficult to prove that this point will be in the lower hull of the candidates points. Plus if we look the slopes with the points in the lower hull, the slope first increase and then decrease so we can do a binary search for find the optimal point. This give us an solution, without doing binary search on doubles!!!. And I think (I'm not quite sure now) that it can be improved to an O(n) since we only need the global optimum.
How do you find a lower hull on interval [i-B, i-A]? you should insert and delete points in CH
The point are added in increasing x coordinate, so we can update the lower hull in O(1) amortized, using a variant of Andres's Scan (page.mi.fu-berlin.de/panos/cg13/l02.pdf). When we move the "windows" we need to delete the leftmost point in the lower hull, we can do it if the hull is stored in a deque. If you need more details, I can be more precise.
It's okay if you can explain what happens in this case :
Ah, I get now what yo meant before. There can be points that can be included in the convex hull when we delete the first point. If there isn't a upper bound in the length of subarray my approach should work. But we can overcome with an extra factor, for an solutions, not so nice tough. The idea is add a point to the convex hull where it can be candidate, we can do it building a segment tree where each node is an lower hull, for query a point we only have to calculate the optimums point in O(logn) hulls. Sorry, it was my mistake.
Can you give a link to the problem?
Here is a similar version http://mirror.codeforces.com/gym/100377/problem/M
I don't have a link for test cases, there is only problem statement.
Here's an O(n)-time algorithm for the problem:
https://arxiv.org/abs/cs/0311020