DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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

Smoothing a Sequence

Back to problem

Editorial

Consider checking whether the answer can be at most DDD.

Let the positions whose original values are kept be i1<i2<⋯<imi_1<i_2<\cdots<i_mi1​<i2​<⋯<im​. If two consecutive kept positions are i<ji<ji<j, it is necessary that

∣Ai−Aj∣≤D(j−i).|A_i-A_j| \le D(j-i).∣Ai​−Aj​∣≤D(j−

This condition is also sufficient. When the difference of the two endpoint values is at most D(j−i)D(j-i)D(j−i), the changed positions between them can be filled with integers so that every step changes by at most DDD. Changed prefixes and suffixes can always be filled as well.

Therefore, it is enough to determine whether at least N−KN-KN−K original values can be kept.

Subtask 4: N≤2000N\le2000N≤2000

Let dpidp_idpi​ be the maximum number of original values we can retain when position iii is the last retained position. By directly checking which earlier positions j<ij<ij<i can be retained together with iii, we obtain

dpi=1+max⁡1≤j<i∣Ai−Aj∣≤D(i−j)dpj.dp_i=1+\max_{\substack{1\le j<i\\|A_i-A_j|\le D(i-j)}}dp_j.dpi​=1+

If no jjj satisfies the condition, take the maximum to be 000.

Compute the states in the order i=1,2,…,Ni=1,2,\ldots,Ni=1,2,…,N and check whether max⁡idpi≥N−K\max_i dp_i\ge N-Kmaxi​dp. One feasibility check takes time. Using it in binary search gives time and space.

For each position iii, define

Xi=Ai−Di,Yi=Ai+Di.X_i=A_i-Di,\qquad Y_i=A_i+Di.Xi​=Ai​−Di,Y

For i<ji<ji<j, the condition above is equivalent to

Xi≥Xj,Yi≤Yj.X_i\ge X_j,\qquad Y_i\le Y_j.Xi​≥Xj​,Yi​

Sort the points (Xi,Yi)(X_i,Y_i)(Xi​,Yi​) by decreasing XiX_iXi, breaking ties by increasing . Let be the length of the longest nondecreasing subsequence of the values in this order. Then is exactly the maximum number of original values that can be kept.

For D>0D>0D>0, since Yi−Xi=2DiY_i-X_i=2DiYi​−Xi​=, any two points satisfying the two coordinate inequalities are also ordered by their original indices. For , we have , and a nondecreasing subsequence after decreasing- sorting can contain several points together only when their values are equal, which is also valid.

Thus DDD is feasible exactly when L≥N−KL\ge N-KL≥N−K. The longest nondecreasing subsequence can be computed in O(Nlog⁡N)O(N\log N)O(NlogN) using upper_bound.

Feasibility is monotone in DDD, so binary-search DDD from 000 to the maximum adjacent difference of the original sequence. The total time complexity is O(Nlog⁡Nlog⁡C)O(N\log N\log C)O(NlogNlogC), where , and the memory complexity is .

Solution written by GPT6

i
)
.
1≤j<i∣Ai​−Aj​∣≤D(i−j)​
max
​
d
pj​
.
i
​
≥
N−
K
O(N2)O(N^2)O(N2)
O(N2log⁡C)O(N^2\log C)O(N2logC)
O(N)O(N)O(N)
i​
=
Ai​+
Di.
≤
Yj​.
​
YiY_iYi​
LLL
YiY_iYi​
LLL
2Di
D=0D=0D=0
Xi=Yi=AiX_i=Y_i=A_iXi​=Yi​=Ai​
XXX
C≤2⋅109C\le 2\cdot 10^9
C≤2⋅109
O(N)O(N)O(N)