dhruvsomani's blog

By dhruvsomani, history, 7 years ago, In English

Hey, I could not understand the solution of a problem and I need some help.

Here's the problem I am talking about. BOI 2009, Candy Machine.

In this solution, I understood the O(n^2) algo with OWSP. Can you please explain what the O(n log n) solution (the code part) is doing.

Why is it sorting on basis of (t + p, t — p)? And what is the binary search on?

Please help. :)

Thanks.

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You do sweep line y=c-x with increasing c. If we maintain the set of wagons, you can see that each wagon can catch candies with y>=w+x. It is optimal to choose the one with wighest w.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Consider each candy falling as an interval like $$$[s - t, s + t]$$$

Claim: $$$j-th$$$ candy can be caught after $$$i-th$$$ candy if and only if $$$s_j - t_j \le s_i - t_i$$$ and $$$s_i + t_i \le s_j + t_j $$$ i.e. $$$j-th$$$ interval contains $$$i-th$$$ interval.

Now the task is to find minimum number of nested intervals to cover all intervals which is a classical Dynamic Programming problem like Longest Increasing Subsequence.