arvindr9's blog

By arvindr9, history, 3 months ago, In English

I tried this problem (AGC 006D, Median Pyramid Hard) a few months ago. The problem gives an array of $$$N$$$ integers ($$$N \leq 10^5$$$), and makes a pyramid over that, where each entry is the median of the three elements below it. The goal is to find the value at the top cell of the pyramid.

I remembered struggling on it for hours. When nothing was working, I was just trying a bunch of random things and decided to try binary searching on the answer (BSTA). Surprisingly it worked!

The idea is to consider asking the question "Is the answer $$$\geq x$$$?". Then you can treat all the elements $$$\geq x$$$ as ones, and all the elements $$$ \lt x$$$ as zeros. The pyramid can be simplified to consist of zeros and ones, and the goal is to check if the top element will equal one, which is much more doable.

This blew my mind, since in the past, I viewed BSTA as a technique for maximization / minimization problems. But here it could be used to calculate something related to a median. I thought this problem was quite cool, so I assigned it as a bonus problem for some of my classes.

It turns out the same technique works for this recent problem (problem D from the recent Spectral Cup, Me When Median Problem). The approach to solve it is pretty much the same as above.

These are the only two problems I recall doing binary search on the answer (and it reduces to a problem over $$$0$$$'s and $$$1$$$'s), where I'm not really trying to maximize / minimize things. Just curious if this kind of approach has been used for other problems too.

  • Vote: I like it
  • +84
  • Vote: I do not like it

»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arvindr9 (previous revision, new revision, compare).

»
3 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

orz

»
3 months ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

I was in arvindr9's class and i was amazed by this technique too. Pretty cool (He also gave extra credits related to this problem to make sure we remember it well :D )

»
3 months ago, hide # |
Rev. 3  
Vote: I like it +8 Vote: I do not like it

This USACO problem uses a similar technique

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Median Splits

This problem related to medians also involves similar idea (replacing elements <= k with 1 and elements > k with -1). however editorial's approach doesnt involve binary search. But the problem is still tagged binary search i wonder why. Maybe somebody can explain the binary search idea here.

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

I was in arvindr9 class and i was amazed by this technique too. Pretty cool (He also gave extra credits related to this problem to make sure we remember it well :D )

»
3 months ago, hide # |
 
Vote: I like it +29 Vote: I do not like it

Also see https://en.wikipedia.org/wiki/Sorting_network#Zero-one_principle, this is the first time I saw the trick documented. To me, I view it as an extension of the binary search technique. They have the same flavor, generally applies the same way (if you look deep enough)

»
3 months ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

I also want to contribute with these problems:

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

The same approach is also used to solve this problem from IOI 2010: Day 1, Quality Of Living

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it
»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

I used this technique to solve this problem, although it is not a median-related problem.

my sol :380125001

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

so tuff now i will be able to solve all median-related problems

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

yes i agree. anyways here's a hot dog

»
3 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Also 1993D is a nice non-trivial one

»
3 months ago, hide # |
Rev. 3  
Vote: I like it +13 Vote: I do not like it

This kind of binarization idea is also useful in lot of counting problem and mostly used together with "Tail-Sum Formula"

$$$E[X] = \sum\limits_{k \gt 0} \Pr[X \ge k]$$$

For r.v. that always evaluate to non-negative integers. ($$$\Pr[X \ge k]$$$ essentially tells you only need to separate elements into "$$$ \lt k$$$" and "$$$\ge k$$$", which is the reason why it fit well with binarization)

For example, this problem.