DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Relay Stations in the Mountains

Back to problem

Editorial

Write combinations as (nr)\binom{n}{r}(rn​), and define the value as 000 when rrr is outside [0,n][0,n][0,n]. All combination values are computed modulo 998 244 353998\,244\,353998244353.

Case D=1D=1D=1

Diameter at most 111 means that every selected pair must communicate directly.

Fix a selected position ppp that is the leftmost among the minimum-height selected positions. Every other selected position qqq must satisfy the following conditions.

  • If p<qp<qp<q, then hq≥hph_q\ge h_phq​≥hp​ and communicate directly.

An equal-height position on the left is forbidden because ppp is the leftmost minimum. One equal-height position on the right may still be selected.

Any subset of these candidate positions forms a clique together with ppp. For example, if p<a<bp<a<bp<a<b and ppp communicates directly with bbb, then hbh_b is larger than every height strictly between and . Therefore and also communicate directly. The other relative orders are proved similarly.

The candidates to the right form a chain. First move to the nearest position on the right whose height is at least hph_php​. Afterwards, repeatedly move to the nearest strictly greater position on the right. The candidates to the left are obtained by repeatedly moving to the previous strictly greater position.

Nearest greater-or-equal and nearest strictly greater positions are found with monotone stacks. The chain lengths are then computed by simple recurrences.

If cpc_pcp​ candidate positions are available for a fixed ppp, they contribute

(cpK−1).\binom{c_p}{K-1}.(K−1cp​​).

Every valid installation is counted exactly once by its leftmost minimum-height relay.

Case D≥2D\ge2D≥2

Let HHH be the maximum selected height, and let the selected positions of height HHH be

p1<p2<⋯<pm.p_1<p_2<\cdots<p_m.p1​<p2​<⋯<p

List all positions whose height is at least HHH in increasing order. The positions p1,…,pmp_1,\ldots,p_mp1​,…,pm​ must be consecutive in this list. Otherwise, an unselected mountain of height at least HH lies between two selected sides and blocks every possible edge across it, so the graph is disconnected.

Let q0q_0q0​ be the previous position before p1p_1p1​ in this list, and let qm+1q_{m+1}q be the next position after . Use and when the corresponding position does not exist. Define

L=p1−q0−1,I=pm−p1−m+1,R=qm+1−pm−1.\begin{align*} L &= p_1-q_0-1,\\ I &= p_m-p_1-m+1,\\ R &= q_{m+1}-p_m-1. \end{align*}LIR​

All lower selected relays must lie between q0q_0q0​ and qm+1q_{m+1}qm+1​. The values LLL, , and count candidates in the left outer region, the internal regions between maximum relays, and the right outer region.

The maximum relays form the path p1,p2,…,pmp_1,p_2,\ldots,p_mp1​,p2​,…,pm​. A relay in an internal gap is adjacent to the neighboring maximum relays. A relay in the left outer region can attach only to , and a relay in the right outer region can attach only to .

Let aaa and bbb indicate whether at least one relay is selected from the left and right outer regions. Since D≥2D\ge2D≥2, distances of at most 222 inside one region require no special handling. The relevant longest path has length

m−1+a+b,m-1+a+b,m−1+a+b,

which must not exceed DDD.

Therefore, after fixing the mmm maximum relays, the number of ways to choose the remaining K−mK-mK−m relays is as follows.

  • If m≤D−1m\le D-1m≤D−1, both outer regions may be used: (L+I+RK−m).\binom{L+I+R}{K-m}.(K−mL+I+R

It remains to enumerate every possible block of maximum relays. Process mountains in decreasing order of height and maintain an ordered set containing positions whose height is at least the current height. Insert all positions of one equal-height group simultaneously. In every consecutive run of the current height in the ordered set, enumerate all contiguous subarrays of length at most D+1D+1D+1.

Each position is the left endpoint of at most D+1D+1D+1 enumerated blocks. The time complexity per test case is

O(Nlog⁡N+ND),O(N\log N+ND),O(NlogN+ND),

and the memory complexity is O(N)O(N)O(N). The D=1D=1D=1 case takes O(N)O(N)O(N) time.

Solution written by GPT5.6

p,qp,q
p,q
  • If q<pq<pq<p, then hq>hph_q>h_phq​>hp​ and p,qp,qp,q communicate directly.
  • hb​
    ppp
    bbb
    aaa
    bbb
    m
    ​
    .
    H
    m+1
    ​
    pmp_mpm​
    000
    N+1N+1N+1
    =p1​−q0​−1,
    =pm​−p1​−m+1,
    =qm+1​−pm​−1.
    ​
    II
    I
    RRR
    p1p_1p1​
    pmp_mpm​
    ​
    )
    .
  • If m=Dm=Dm=D, the two outer regions may not both be nonempty: (I+RK−m)+(L+IK−m)−(IK−m).\binom{I+R}{K-m}+\binom{L+I}{K-m}-\binom{I}{K-m}.(K−mI+R​)+(K−mL+I​)−(K−mI​).
  • If m=D+1m=D+1m=D+1, neither outer region may be used: (IK−m).\binom{I}{K-m}.(K−mI​).
  • If m>D+1m>D+1m>D+1, the choice is impossible.