B - Traffic Light 解説 by evima
Let the balance when the button is pressed at time \(i\) be \(W_i\) yen. The problem asks to choose some elements from \(W\) with intervals at least \(Y\) to maximize the sum.
Let \(dp[i]\) be the answer to the problem when considering up to \(W_i\). Then, \(dp[i] = \max(dp[i-1],dp[i-Y]+W_i)\). Let us perform this transition quickly.
Consider managing the last \(Y\) elements of \(\mathrm{dp}\). Suppose we currently have \(dp[i-Y+1],dp[i-Y+2],\dots,dp[i]\) and want to process a segment where \(W_{i+1} = W_{i+2} = \dots = W_{i+k} = V\). (Note that \(dp\) is monotonically increasing.) First, if \(V \le 0\), then \(dp[i+1]\) through \(dp[i+k]\) all become \(dp[i]\). Otherwise, for simplicity assume \(k \le Y\). (Otherwise, we can divide it into several segments of length at most \(Y\). Even with this operation, the number of segments can be kept at \(\mathrm{O}(N)\).) Then, \(dp[i+j] = \max(dp[i+j-Y] + V,dp[i-1])\).
The above operations can all be processed by rotation of the sequence, range addition, and range update. For rotation, if we manage how much we have rotated currently, we only need to be able to perform range addition and range update on the sequence.
As is, we would perform query processing on a sequence of length \(Y\), but noting that the necessary parts of \(dp\) are only where the value of \(W\) changes, we only need to manage \(\mathrm{O}(N)\) positions in the end. Thus, we can solve this problem in \(\mathrm{O}(N \log N)\).
投稿日時:
最終更新: