by zkw, Jul 18, 2016, 3:08 PM
This is an

approach. Let

be the maximal length of

starting from

and going to direction
. Let

then the answer should be

Divide the problem with different
, and this becomes an 1D problem. Given two arrays

and
, try to pick an index

from
, and

from
, such that
![$j<i+a[i]$](//latex.artofproblemsolving.com/9/4/3/94322c4286be5242e7a474ea4f25a403ed5e5e39.png)
and
, which leads to the maximal
. Let
![$v[\mathbf{a}]$](//latex.artofproblemsolving.com/d/6/d/d6d4f384bea495741015470a7cbf1665cbe3e06c.png)
be the set of all intervals
, and
![$v[\mathbf{b}]$](//latex.artofproblemsolving.com/d/3/8/d38bf817393903192307a659a54a71ed2bbe1324.png)
be the set of all intervals
. It is easy to find that if an interval in
![$v[\mathbf{a}]$](//latex.artofproblemsolving.com/d/6/d/d6d4f384bea495741015470a7cbf1665cbe3e06c.png)
is covered by another interval in
, then it is useless. So we can sort all intervals in
, such that the left ends and right ends are both increasing, and the same as
. For each intervals
![$[p,q]$](//latex.artofproblemsolving.com/7/5/7/757cf73e34e24c5f0bd122a6f1e90cfd30fcc0c5.png)
in sorted
, discard all intervals
![$[r,s]$](//latex.artofproblemsolving.com/8/8/1/881d86b71d64dc3cb4937c4ef7eaf28c0e73cde9.png)
such that

or

in
, and updates the answer accordingly.
Program (in C++)
This post has been edited 2 times. Last edited by zkw, Jul 18, 2016, 3:11 PM