5

Bin search and relative error

 2 years ago
source link: http://codeforces.com/blog/entry/49189
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Bin search and relative error

By Um_nik, history, 5 years ago,

Suppose we want to solve a problem by doing binary search on answer. Then the answer will be checked against jury's answer by absolute or relative error (one of them should be smaller then ε). For simplicity we will assume that our answer is always greater than 1 and smaller than B. Because of that, we will always use relative error rather than absolute.

Suppose we have made n iterations of our binary search — what information do we have now? I state that we know that real answer is lying in some segment [xi, xi + 1], where 1 = x0 < x1 < ... < xi < ... < x2n = B. And what is great — we can choose all xi except for x0 and x2n.

Now, for simplicity, we will also assume that we will answer xi + 1 for segment [xi, xi + 1] and the real answer was xi — it is the worst case for us. It is obvious that we will not do that in real life, any other answer would be better, but you will get the idea.

So, what is our relative error? It is 17b160534b94e24316671b9e0158bbbf0c561666.png. Worst case for us is when relative error is maximal. It is logical to make them equal — exactly what we do by binary search with absolute errors. 69ba9daae2b3637649588982707192d4a7b8b13e.png. We can assume that 7455406c3af15db10fc901456ab13e8fb94b72f9.png so ad249b94b0e4e3407dd2b0f4ca56cfd1450b8ac8.png. Now we have 187766315b8aa6dc6ea72b8bf8e271b4619c0e0a.png, but d74c180f3001a4cfaea0e0934943c599ee1fb7d9.png, so

Unable to parse markup [type=CF_TEX]

. How large should be n to get error less than ε? e4c02800553cee87ed1aec431518d5488dfd50a7.png. Much smaller than 9d96c0a00fafe600b212c4ac656e4319ed8adf74.png.

How to write such binary search? We want to choose m in such a way that 1a2935ec023e94f58becf8b61f396134901426e0.png or simply 2914a7bf644265d323326ec307171b7288b62aad.png.

Now I want to deal with some assumptions I made.

How to choose answer in the end? Again, c9079e4f6a72d17e335de89fe25f5c81a4283795.png (it is basically the same as dividing the segment in binary search).

What to do if the answer can be smaller than 1? Try 1; if answer is smaller than 1~--- use standard binary search (because absolute error smaller than relative); is answer is bigger than 1~--- use the binary search above.

P.S. I have never heard about this idea and come up with this while solving 744D - Hongcow Draws a Circle. I'm sorry if it is known for everyone except for me.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK