E - Doubling Editorial
by
apiad
Editorial for printing
O(n^2k) Solution
Maybe it is the same as maspy’s solution.
First, apply inclusion–exclusion. For a consecutive run of violated conditions, we have:
- \(c_{i-1}\) is unrestricted.
- \(c_i=2c_{i-1}+X_i+1+a_i\).
- \(c_{i+1}=2c_i+X_{i+1}+1+a_{i+1}\).
- \(\ldots\)
Here, each \(a_i\ge 0\).
If the conditions at positions \(i,\ldots,i+j-1\) are violated, the contribution of this part, including \(c_{i-1}\), to \(\sum c_i\) is
\[ c_{i-1}(2^{j+1}-1) +(X_i+1+a_i)(2^j-1) +\cdots +(X_{i+j-1}+1+a_{i+j-1})(2^1-1). \]
Next, consider a DP. Let \(dp_{i,j,k}\) denote the state after processing position \(i\), where the coefficient of the current number’s contribution is \(2^{j+1}-1\), and the total is \(k\).
The coefficient of the preceding number can be \(1\), meaning that the current number starts a new block, or \(2^{j+2}-1\), meaning that it extends the preceding block.
Writing \(w_j=2^{j+1}-1\) and summing over \(t\), we obtain
\[ dp_{i,j,k} = \sum_{t\ge 0}dp_{i-1,0,k-tw_j} - \sum_{t\ge X_i+1}dp_{i-1,j+1,k-tw_j}. \]
In the first sum, \(t=c_i\); in the second sum, \(t=c_i-2c_{i-1}\). States with negative sum indices are zero.
These sums can be optimized using prefix sums with step size \(w_j\). The overall time complexity is \(O(n^2k)\).
posted:
last update: