Can anybody explain me how to solve E using binnary search(I can`t understand editorial)
# | User | Rating |
---|---|---|
1 | tourist | 3880 |
2 | jiangly | 3669 |
3 | ecnerwala | 3654 |
4 | Benq | 3627 |
5 | orzdevinwang | 3612 |
6 | Geothermal | 3569 |
6 | cnnfls_csy | 3569 |
8 | jqdai0815 | 3532 |
9 | Radewoosh | 3522 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | awoo | 161 |
2 | maomao90 | 160 |
3 | adamant | 157 |
4 | maroonrk | 154 |
5 | -is-this-fft- | 148 |
5 | atcoder_official | 148 |
5 | SecondThread | 148 |
8 | Petr | 147 |
9 | TheScrasse | 145 |
9 | nor | 145 |
Name |
---|
Note: I use 1-indexing and a translator.
Idea: let's go through all possible subarrays [i..j], and if sum of elements is equal to s, then recalculate the answer: ans = min(ans, i — 1 + n — j) — that is, we need to remove (i — 1) elements from the beginning and (n — j) from the end.
This is O(n^2), we can improve with binary search:
Let's count the prefix sums in the array a, and go through j. Then we need to find the leftmost i so that (a[j] — a[i — 1]) == s, in other words, find the leftmost (a[j] — s) in the prefix-sum array. Then the answer is recalculated in the same way as in the case of O(n^2).
Now O(n log n).
My solution: link
Why downvotes? I only help with solution!
thanks very much :) You realy help me bro)