Блог пользователя Errichto

Автор Errichto, 7 лет назад, По-английски

I recorded a 30-minute lecture about binary search. It should allow you to really understand it and stop guessing about +1 in random places. Enjoy.

https://www.youtube.com/watch?v=GU7DpgHINWQ

Consider watching with captions on and with x1.25 speed.

  • Проголосовать: нравится
  • +233
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +34 Проголосовать: не нравится

ETA — Blogewoosh #8?

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thank you, but you didn't mention invariants.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please tell how to approach for problems such as https://mirror.codeforces.com/contest/1169/problem/C

  • »
    »
    7 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Code: 54682960

    Observe that the problem is basically asking for the most number of times that a number has to change.

    The problem asks to find the minimum number of operations, which is bounded from 1 to m. Thus, you can binary search for the minimum number of operations in O(log(m)) time.

    That means that you can dedicate O(N) time to checking if it is possible to sort the array using <= a certain amount of operations.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -20 Проголосовать: не нравится

Why you don't do analysis of all next educational rounds problems? They are cool to learn topics but sometimes they are hard to understand even with the editorial

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +189 Проголосовать: не нравится

I am just going to leave this here.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can you make a similar video on topics like RMQ and Geometry as many people struggle in those topics?

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Finally 1st.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In case of finding maximum in first increasing and then decreasing array if duplicate elements are present how to handle this?

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -13 Проголосовать: не нравится

Really great tutorial , I suggest making such tutorials in number theory topics like segmented sieve or search techniques like ternary search.

I have also implemented the code for the problem to find the maximum value in a mountain range . this is my code , can you kindly review my code if there is any bugs ?

https://ideone.com/5YgGqF

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thank you Errichto for the great lecture.

However I'm concerned about binary search on double:

const double target = 1e18;
double L = 0, R = target;
while (R - L > 1e-9) {
    double mid = L + (R - L) / 2;
    if (mid * mid < target)
        L = mid;
    else
        R = mid;
}

I think previous code won't stop, as when L and R get near the sqrt (which is 1000000000), numbers will be large so precision will be low and comparision (R — L > 1e-9) will never be false.

I prefer use L and Sz (size of the interval), instead of L and R as Sz will always get halved each time, so it's guaranteed to stop.

const double target = 1e18;
double L = 0, Sz = target;
while (Sz > 1e-9) {
    double mid = L + Sz / 2;
    if (mid * mid < target)
        L = mid;
    Sz /= 2;
}

We can also limit the iterations by log of the number.

  • »
    »
    7 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    The thing is it's impossible to get precision $$$10^{-9}$$$ for that big numbers anyway. The precision of your code is for sure bigger than $$$10^{-9}$$$. In a problem from a contest, either values will be smaller or they will ask for relative or absolute error and then we should check that instead of just an absolute error.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi Errichto, When there will be a lecture on dp?

»
3 года назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Can someone explain me where to use left+1 and Right -1 or not to use -1 or +1 . Watched the video but couldn't find any solution to this problem :) ;