E - 上下に跳ぶ展示順 / Exhibition Order Jumping Up and Down Editorial by admin
gpt-6-astra-mediumSummary
We want to select exhibits while maintaining their original index order to form a sequence where the height difference between adjacent exhibits falls within a specified range, and the heights alternate between increasing and decreasing, in order to maximize the total score.
We consider dynamic programming where the state tracks “whether the last transition was an increase or a decrease,” and accelerate it using coordinate compression and segment trees.
Analysis
Remembering the Direction of the Last Transition
When exhibit \(i\) is chosen as the last item, the next exhibits that can be chosen depend on whether the transition immediately before was an increase or a decrease. Therefore, we define the following two states:
- \(U_i\): the maximum score of a sequence ending at exhibit \(i\) where the last transition is an increase (up)
- \(D_i\): the maximum score of a sequence ending at exhibit \(i\) where the last transition is a decrease (down)
However, a sequence consisting of only exhibit \(i\) is included in both states. This allows the first transition to be either an increase or a decrease.
Transitions Can Be Represented as Height Ranges
To choose exhibit \(i\) after exhibit \(j\) such that the last transition is an increase, we need:
- \(j<i\)
- The previous last transition was a decrease
- \(L\leq A_i-A_j\leq R\)
Rewriting the condition on heights gives:
\(A_i-R\leq A_j\leq A_i-L\)
Therefore, the transition formula is as follows:
\(U_i=B_i+\max\left(0,\ \max_{\substack{j<i\\A_i-R\leq A_j\leq A_i-L}}D_j\right)\)
Similarly, when the last transition is a decrease:
\(D_i=B_i+\max\left(0,\ \max_{\substack{j<i\\A_i+L\leq A_j\leq A_i+R}}U_j\right)\)
If no candidate \(j\) exists, the maximum is taken as \(0\). This \(0\) corresponds to the choice of “not using any previous sequence and starting a new sequence with only exhibit \(i\).”
For example, if \(A_i=10,\ L=2,\ R=4\):
- The height immediately before reaching \(i\) via an increase is in \([6,8]\)
- The height immediately before reaching \(i\) via a decrease is in \([12,14]\)
It is sufficient to know the maximum value of the opposite-direction state in each of these intervals.
Speeding Up the Naive DP
Examining all previous \(j\) for each \(i\) takes \(O(N^2)\) time. This will not pass within the time limit for \(N\leq 200000\).
On the other hand, the only thing needed for transitions is “the maximum DP value in a specified range of heights.” This can be handled by a segment tree that supports range maximum queries.
Although the heights can be as large as \(10^9\), at most \(N\) distinct heights actually appear, so we can manage them using coordinate compression.
Algorithm
1. Coordinate Compress the Heights
Sort all \(A_i\) and remove duplicates to create an array heights. Let its length be \(M\), and map each height to an index from \(0\) to \(M-1\).
Since the actual differences in height cannot be determined from the compressed indices alone, we compute the endpoints of the intervals using the original heights and convert them to indices using binary search.
The half-open interval of indices corresponding to the closed interval \([x,y]\) is:
\([\operatorname{lower\_bound}(x),\operatorname{upper\_bound}(y))\)
2. Prepare Two Segment Trees
up: maintains the maximum of processed \(U_j\) for each heightdown: maintains the maximum of processed \(D_j\) for each height
Both are initialized with \(0\), and updates store the maximum with the current value.
3. Process Exhibits in Index Order
For \(i=1,2,\ldots,N\) in order, perform the following operations:
- Query the range maximum for heights in \([A_i-R,A_i-L]\) from
down, and add \(B_i\) to obtain \(U_i\). - Query the range maximum for heights in \([A_i+L,A_i+R]\) from
up, and add \(B_i\) to obtain \(D_i\). - At the position corresponding to height \(A_i\), update
upwith \(U_i\) anddownwith \(D_i\) by taking the maximum. - Update the answer with \(U_i\) and \(D_i\).
Since we process exhibits in index order, the segment trees only contain states for \(j<i\) when queried.
4. Output the Maximum Value
Since the score when choosing nothing is \(0\), the answer is
\(\max\left(0,\max_i U_i,\max_i D_i\right)\)
Why This Approach is Correct
If we remove the last exhibit \(i\) from a valid sequence of length \(2\) or greater, the remaining sequence becomes a state whose last transition was in the opposite direction. Furthermore, the height of the immediately preceding exhibit is always included in the interval specified in the transition formula.
Conversely, appending exhibit \(i\) to an opposite-direction state in that interval satisfies both the height difference and alternating conditions. Sequences of length \(1\) are also accounted for via \(0+B_i\).
Therefore, each transition exhaustively considers all valid choices, and the maximum across all states and \(0\) gives the correct answer.
Complexity
- Time complexity: \(O(N\log N)\)
- Space complexity: \(O(N)\)
Coordinate compression takes \(O(N\log N)\), and binary searching, range maximum querying, and updating for each exhibit take \(O(\log N)\) time.
Implementation Notes
The initial values of the segment trees can be \(0\)
In the transitions, we always take the maximum with \(0\). Since starting fresh with only the current exhibit yields a higher score than extending a sequence with a negative total score, there is no need to store negative DP values. However, this does not mean that exhibits with negative scores \(B_i\) can be ignored altogether.Update states of the same height with the maximum value
Even at the same height, a state with a higher score may already exist from earlier exhibits. Theupdateoperation in the code stores the maximum with the current value rather than overwriting it.Compute both states before updating
Computing both \(U_i\) and \(D_i\) first and then applying them to the segment trees makes the flow of “only transitioning from already processed exhibits” clear.Include both endpoints of the interval
The condition is \(L\leq |A_i-A_j|\leq R\). Uselower_boundfor the left endpoint andupper_boundfor the right endpoint. If there are no heights within the range, the query result should be \(0\).Use 64-bit integers for the scores
Since the sum of scores can reach up to \(2\times 10^{14}\),long longis used in the code.Source Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
class SegmentTree {
int size;
vector<ll> tree;
public:
explicit SegmentTree(int n) {
size = 1;
while (size < n) size <<= 1;
tree.assign(size * 2, 0);
}
void update(int pos, ll value) {
pos += size;
if (tree[pos] >= value) return;
tree[pos] = value;
while (pos > 1) {
pos >>= 1;
ll next = max(tree[pos << 1], tree[pos << 1 | 1]);
if (tree[pos] == next) break;
tree[pos] = next;
}
}
ll query(int left, int right) const {
ll result = 0;
for (left += size, right += size; left < right;
left >>= 1, right >>= 1) {
if (left & 1) result = max(result, tree[left++]);
if (right & 1) result = max(result, tree[--right]);
}
return result;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
ll L, R;
cin >> N >> L >> R;
vector<ll> A(N), B(N), heights;
heights.reserve(N);
for (int i = 0; i < N; ++i) {
cin >> A[i] >> B[i];
heights.push_back(A[i]);
}
sort(heights.begin(), heights.end());
heights.erase(unique(heights.begin(), heights.end()), heights.end());
int M = static_cast<int>(heights.size());
SegmentTree up(M), down(M);
auto lower = [&](ll x) -> int {
return lower_bound(heights.begin(), heights.end(), x) - heights.begin();
};
auto upper = [&](ll x) -> int {
return upper_bound(heights.begin(), heights.end(), x) - heights.begin();
};
ll answer = 0;
for (int i = 0; i < N; ++i) {
ll bestUp = B[i] + down.query(lower(A[i] - R), upper(A[i] - L));
ll bestDown = B[i] + up.query(lower(A[i] + L), upper(A[i] + R));
int pos = lower(A[i]);
up.update(pos, bestUp);
down.update(pos, bestDown);
answer = max({answer, bestUp, bestDown});
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.