AGC019D 爆标做法(English ver.)
Translated by DeepL with my manual modification.
Firstly, there is no solution if and only if \(B_i\equiv0\) and there exists \(A_i=1\).
It can always be found that any one of the optimal routes takes the form of a total of \(l\) steps to the left and \(r\) steps to the right, with the end point \(p\) to the left/right of the starting point.
This is easily proved by tweaking, and the answer is \(2r+2l-p+\text{number of unmatched pairs at the endpoints}\), where "unmatched pairs" is defined as either \(0\) and \(1\) at the same subscript, or \(1\) and \(0\) at the same subscript.
We might as well enumerate the endpoints and minimize \(l+r\).
Then a scheme is legal if and only if any of the \(A_i=1\) numbers have ever been at \(B_i=1\) (including the endpoints).
This translates into no more than \(n\) restrictions, each of the form \(l\ge l_j\lor r\ge r_j\), where \(l_j/r_j\) denotes how far to at least \(1\) in \(B\) it would be if it were only to go left/right; \(l\ge p\) for a restriction to the left, and \(r\ge p\) for a restriction to the right; and finally, \(l\ge0\) and \(r\ge0\). and \(r\ge0\).
In this way we are planning as follows:
Consider solving the plan by graphing.
We draw these restrictions through to the 2D plane, which is a number of \(3/4\) planes taking intersections with two half-planes to find the minimum \(x+y\) on them.
Noting that \(z=x+y\) is a diagonal line when \(z\) is constant, we consider just finding the minimum legal point on the contour line.
This can be handled directly by bucket rows and then monotonic stacks. Total complexity \(O(n^2)\).
If you notice that the previous constraints are always the same, this problem can actually be done with a scan line to achieve a better complexity!
The total complexity will be \(O(n\log n)\) due to the FFT acceleration of the previous "number of unmatched pairs" contribution.
本文来自博客园,作者:myee,转载请注明原文链接:https://www.cnblogs.com/myee/p/AGC019D-Onlognsol-Englishver.html