E - ReTravel Editorial
by
tassei903
This problem can be solved using interval DP.
Let point \(0\) be defined as \((0, 0)\).
For \(0 \leq l \leq r \leq N\), define \(P_{l, r} = (X(l, r), Y(l, r)) = \left(\min_{l \leq i \leq r} X_i, \min_{l \leq i \leq r} Y_i\right)\).
Let \(dp[l][r]\) represent the minimum cost to visit all points from \(l\) to \(r\) when starting from \(P_{l, r}\) with an empty string \(S\). The answer we seek is \(dp[0][n]\).
The minimum cost to visit points \(l, \dots, r\) for the first time starting from \((x, y)\), with an empty string \(S\), is calculated as follows:
- If \(x \leq X(l, r)\) and \(y \leq Y(l, r)\), then the cost is
\( X(l, r) - x + Y(l, r) - y + dp[l][r] \) - Otherwise, it is impossible.
To compute the cost of moving through \(P_{l, r}\) while visiting all points from \(l\) to \(r\), we consider every possible time \(P_{l, r}\) is visited. Specifically, for \(l \leq i < r\), we calculate the minimum cost as follows:
- Visit all points from \(l\) to \(i\), pass through \(P_{l, i}\), and return to \(P_{l, r}\).
- Then, visit all points from \(i+1\) to \(r\), pass through \(P_{i+1, r}\), and return to \(P_{l, r}\).
Thus, the DP recurrence is as follows:
- When \(l = r\), \(dp[l][r] = 0\).
- When \(l < r\), \(dp[l][r] = \min_{l \leq i < r} \left(dp[l][i] + dp[i+1][r] + X(l, i) + X(i+1, r) - 2X(l, r) + Y(l, i) + Y(i+1, r) - 2Y(l, r)\right)\)
Using this approach, the problem can be solved in \(O(N^3)\) time.
posted:
last update:
