@@ -105,15 +105,15 @@ $\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
- $\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))$
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
- $\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])$
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
- $\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])$
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
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
@@ -127,15 +127,16 @@ using namespace std;
127
127
#define f first
128
128
#define s second
129
129
#define pb push_back
130
- #define rep(i, a, b) for(int i = a; i < (b); ++i)
130
+ #define rep(i, a, b) for (int i = a; i < (b); ++i)
131
131
#define all(x) begin(x), end(x)
132
132
#define sz(x) (int)(x).size()
133
133
typedef long long ll;
134
134
typedef pair<int, int > pii;
135
135
typedef vector<int > vi;
136
136
137
- template<class T > bool ckmin(T& a, const T& b) {
138
- return b < a ? a = b , 1 : 0 ; } // set a = min(a,b)
137
+ template <class T > bool ckmin(T &a, const T &b) {
138
+ return b < a ? a = b , 1 : 0 ;
139
+ } // set a = min(a,b)
139
140
140
141
/**
141
142
* Description: 1D point update, range query where \texttt{ comb } is
@@ -185,7 +186,8 @@ int y_index(int y) {
185
186
}
186
187
187
188
int main() {
188
- ios_base::sync_with_stdio (0 ); cin .tie (0 );
189
+ ios_base::sync_with_stdio (0 );
190
+ cin .tie (0 );
189
191
freopen (" boards.in" , " r" , stdin );
190
192
freopen (" boards.out" , " w" , stdout );
191
193
cin >> N >> P ;
@@ -210,7 +212,9 @@ int main() {
210
212
ins (y_index (t .f .s ), ans [t .s .f ] - t .f .f - t .f .s );
211
213
}
212
214
}
213
- cout << query_min (2 * P - 1 ) + 2 * N ; // adds 2N to include the transition from the last springboard to the endpoint
215
+ cout << query_min (2 * P - 1 ) +
216
+ 2 * N ; // adds 2N to include the transition from the last
217
+ // springboard to the endpoint
214
218
}
215
219
` ` `
216
220
0 commit comments