@@ -97,25 +97,25 @@ int main() {
97
97
98
98
The first step is to create a DP to solve the first subtask. The states are the springboards and the transitions are between springboards. First, sort the springboards by the pair $(x_1, y_1)$ in increasing order. It is possible to show that for all $i$, $j$, where $i < j$, Bessie cannot use springboard $j$ then $i$ later.
99
99
100
- For each springboard $i$, let $ans[i]$ denote the minimum distance needed to walk to the start point of springboard $i$.
100
+ For each springboard $i$, let $\texttt{ ans} [i]$ denote the minimum distance needed to walk to the start point of springboard $i$.
101
101
102
- Let $dist(a, b)$ be the walking distance from the end of springboard $a$ and the start of springboard $b$.
102
+ Let $\texttt{ dist} (a, b)$ be the walking distance from the end of springboard $a$ and the start of springboard $b$.
103
103
104
- $dist(a, b) = x_1[b] + y_1[b] - x_2[a] - y_2[a]$
104
+ $\texttt{ dist} (a, b) = x_1[b] + y_1[b] - x_2[a] - y_2[a]$
105
105
106
106
Then, the transitions are:
107
107
108
- $ans[i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + dist(j, i))$
108
+ $\texttt{ ans} [i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + \texttt{ dist} (j, i))$
109
109
110
110
**Full Solution:** $\mathcal{O}(P \log P)$
111
111
112
112
Optimizing the DP involves the use of a min point update range query segment tree. Let's first expand $dist(i, j)$ in the transition formula.
113
113
114
- $ans[i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + x_1[i] + y_1[i] - x_2[j] - y_2[j])$
114
+ $\texttt{ ans} [i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(\texttt{ ans} [j] + x_1[i] + y_1[i] - x_2[j] - y_2[j])$
115
115
116
- $ans[i] = x_1[i] + y_1[i] + \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] - x_2[j] - y_2[j])$
116
+ $\texttt{ ans} [i] = x_1[i] + y_1[i] + \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(\texttt{ ans} [j] - x_2[j] - y_2[j])$
117
117
118
- We notice that everything inside the $\min$ statement only depends on $j$. The segment tree stores $ans[j] - x_2[j] - y_2[j]$ at index $y_2[j]$. We can seperate the start and end of springboards to create two seperate events for each springboard, still sorting by $(x, y)$. When the event is the start of a springboard, update $ans[i]$ through a segment tree query. When the event is the end of a springboard, update the segment tree.
118
+ We notice that everything inside the $\min$ statement only depends on $j$. The segment tree stores $\texttt{ ans} [j] - x_2[j] - y_2[j]$ at index $y_2[j]$. We can seperate the start and end of springboards to create two seperate events for each springboard, still sorting by $(x, y)$. When the event is the start of a springboard, update $ans[i]$ through a segment tree query. When the event is the end of a springboard, update the segment tree.
119
119
120
120
By processing in order, the first two conditions in the $\min$ statement are always satisfied. The third is where the segment tree comes into play, where querying the range $[0, y_1[i]]$ is sufficent to satisfy all constraints.
121
121
0 commit comments