E - 上下に跳ぶ展示順 / Exhibition Order Jumping Up and Down Editorial by admin
gpt-6-astra-mediumOverview
We choose exhibits in increasing order of their indices, alternating between increasing and decreasing heights, to maximize the total score. We consider dynamic programming maintaining the “last movement direction” as a state, and accelerate it using coordinate compression and segment trees.
Analysis
Remembering the Last Movement Direction
When exhibit \(i\) is chosen last, which exhibit can be chosen next depends on whether the height increased or decreased immediately before.
Therefore, we define the following two states:
- \(U_i\): the maximum score of a selection that ends at exhibit \(i\), with the last movement being an increase
- \(D_i\): the maximum score of a selection that ends at exhibit \(i\), with the last movement being a decrease
However, the case of choosing only exhibit \(i\) is included in both states. This allows the first movement to be either an increase or a decrease.
Transition Sources Can Be Represented by a Range of Heights
Consider the case where exhibit \(i\) is chosen after exhibit \(j\), ending with an upward movement (increase).
Since exhibits are chosen in order of their indices, we require \(j<i\), and for the heights:
\(L\leq A_i-A_j\leq R\)
is required. Rewriting this in terms of the range of \(A_j\), we get:
\(A_i-R\leq A_j\leq A_i-L\)
Also, to alternate the movement directions, the transition must come from \(D_j\).
Similarly, for the case ending with a downward movement (decrease), the transition comes from \(U_j\) whose height satisfies:
\(A_i+L\leq A_j\leq A_i+R\)
Therefore, the transition equations are 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)\)
\(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 there is no corresponding exhibit, the inner maximum is set to \(-\infty\). Taking the maximum with \(0\) accounts for the option of starting freshly with only exhibit \(i\) without connecting to past exhibits.
For example, if \(A_i=10,\ L=2,\ R=4\):
- To reach \(i\) via an increase, we look at decrease states with heights between \(6\) and \(8\) inclusive.
- To reach \(i\) via a decrease, we look at increase states with heights between \(12\) and \(14\) inclusive.
Accelerating 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\).
All we need from past states is the maximum value among those whose height falls into a specified range. This operation can be sped up using a segment tree that computes range maximum queries.
Since heights can be up to \(10^9\), they cannot be directly used as array indices. Therefore, we coordinate-compress only the heights that actually appear.
Algorithm
1. Coordinate Compress Heights
Collect all \(A_i\), sort them, and remove duplicates to create an array xs.
Manage each height by its index in xs. The difference conditions are computed using the uncompressed heights, and the corresponding index range in xs is found using binary search.
2. Prepare a Segment Tree for Each Direction
Prepare the following two segment trees:
up: the maximum value of \(U\) among states ending at each heightdn: the maximum value of \(D\) among states ending at each height
Initialize positions where no states exist yet to \(-\infty\).
Even if multiple exhibits have the same height, it is sufficient to keep only the maximum score for the same direction. Since the conditions for exhibits that can be connected in the future are identical, there is no advantage to keeping a state with a lower score.
3. Process Exhibits in Order of Their Indices
For \(i=1,2,\ldots,N\) in this order, perform the following steps:
- Query
dnfor the maximum value among states with heights in \([A_i-R,A_i-L]\). - Add \(B_i\) to the larger of the queried value and \(0\), and set this as \(U_i\).
- Query
upfor the maximum value among states with heights in \([A_i+L,A_i+R]\). - Add \(B_i\) to the larger of the queried value and \(0\), and set this as \(D_i\).
- Update the position corresponding to height \(A_i\) with \(U_i\) in
upand with \(D_i\) indn(taking the maximum).
By processing in order of indices, the trees will only contain states created from exhibits \(j<i\) at the time of each query.
4. Compute the Answer
Since the score is \(0\) when no exhibits are chosen, the answer is:
\(\max\left(0,\max_i U_i,\max_i D_i\right)\)
Why This Is Correct
A valid selection ending at exhibit \(i\) either consists of exhibit \(i\) alone or appends \(i\) to an earlier selection.
When appending \(i\), if the last move is an increase, it must transition from a “decrease state at the specified lower height range”; if the last move is a decrease, it must transition from an “increase state at the specified higher height range”. The transition equations examine all such states. Conversely, appending \(i\) to any state satisfying these conditions is always valid.
Therefore, by computing these transitions in order of the indices, we can correctly find the maximum score for all states.
Complexity
- Time Complexity: \(O(N\log N)\)
- Space Complexity: \(O(N)\)
Coordinate compression takes \(O(N\log N)\), and for each exhibit, a constant number of binary searches and segment tree operations take \(O(\log N)\).
Key Implementation Points
Converting closed intervals to half-open intervals
The code’sget(l, r)queries the half-open index interval \([l,r)\). The range corresponding to the closed height interval \([x,y]\) is fromlower_bound(xs, x)toupper_bound(xs, y). This properly includes cases where the difference is exactly \(L\) or \(R\).Update using maximum, not overwrite
Because the same height may appear multiple times, ensure that a better state already registered is not lost. In the code,addrepresents updating with the maximum rather than addition.Calculate both states before updating
Updating the segment trees after computing both \(U_i\) and \(D_i\) clarifies the intended logic that “transitions only come from strictly previous exhibits.”Distinguish negative scores from choosing nothing
Since \(U_i\) and \(D_i\) must include exhibit \(i\), they can be negative. There is no need to clamp these values to at least \(0\) themselves. Initializing the answer to \(0\) correctly handles the case where all scores are negative by choosing nothing.Use 64-bit integers
Since the total score can reach up to \(2\times 10^{14}\),long longis required. In the code,-(1LL << 60)is used as a sufficiently small value to represent non-existent states.Source Code
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll NEG=-(1LL<<60);
struct Seg {
int s; vector<ll> t;
Seg(int n){s=1;while(s<n)s*=2;t.assign(2*s,NEG);}
void add(int i,ll x){i+=s;t[i]=max(t[i],x);for(i/=2;i;i/=2)t[i]=max(t[2*i],t[2*i+1]);}
ll get(int l,int r){ll v=NEG;for(l+=s,r+=s;l<r;l/=2,r/=2){if(l&1)v=max(v,t[l++]);if(r&1)v=max(v,t[--r]);}return v;}
};
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),xs;
for(int i=0;i<n;i++){cin>>a[i]>>b[i];xs.push_back(a[i]);}
sort(xs.begin(),xs.end());xs.erase(unique(xs.begin(),xs.end()),xs.end());
Seg up(xs.size()),dn(xs.size());ll ans=0;
auto lo=[&](ll x){return int(lower_bound(xs.begin(),xs.end(),x)-xs.begin());};
auto hi=[&](ll x){return int(upper_bound(xs.begin(),xs.end(),x)-xs.begin());};
for(int i=0;i<n;i++){
ll u=b[i]+max(0LL,dn.get(lo(a[i]-R),hi(a[i]-L)));
ll d=b[i]+max(0LL,up.get(lo(a[i]+L),hi(a[i]+R)));
int z=lo(a[i]);up.add(z,u);dn.add(z,d);ans=max({ans,u,d});
}
cout<<ans<<'\n';
}
This editorial was generated by gpt-6-astra-medium.