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

서로 다른 구간 지우기

Back to problem

Editorial

Suppose a value xxx appears MMM times in the sequence. Since every deleted segment must have pairwise distinct elements, one operation cannot delete two or more occurrences of xxx.

Therefore, to delete one occurrence of xxx, the same operation must also delete at least one element different from xxx. There are only N−MN-MN−M elements different from xxx, so at most N−MN-MN−M occurrences of xxx can be deleted. Hence at least

M−(N−M)=2M−NM-(N-M)=2M-NM−(N−M)=2M−N

occurrences of xxx must remain.

Taking the maximum over all values, if MMM is the maximum frequency of any value, the answer is at least

max⁡(0,2M−N).\max(0,2M-N).max(0,2M−N).

We now show that this lower bound is always achievable.

First, suppose M>N/2M>N/2M>N/2. Let xxx be a most frequent value. As long as there is an element different from xxx, there must be an adjacent pair consisting of one xxx and one non-xxx element. These two elements are distinct, so we can delete this segment of length . Repeating this removes all non- elements and the same number of elements. Thus exactly elements remain.

Now suppose M≤N/2M \le N/2M≤N/2. We claim that all elements can be deleted. This can be proved by induction on the current length of the sequence.

Let the current length be nnn and let MMM be the current maximum frequency.

  • If n=2n=2n=2, the two elements must be distinct, so they can be deleted at once.
  • If the maximum frequency is small enough, deleting any adjacent unequal pair keeps every frequency at most half of the new length, so the induction hypothesis applies.
  • If a value has frequency close to half, there is a boundary where that value is adjacent to a different value. Deleting this pair decreases the critical frequency.
  • The only delicate case is when nnn is odd and two different values each appear (n−1)/2(n-1)/2(n−1)/2 times. If those two values are adjacent, delete that adjacent pair. Otherwise, the single remaining value lies between them somewhere, so there is a segment of length containing three pairwise distinct values. Delete that segment. In both cases, the induction hypothesis applies afterward.

Therefore, when M≤N/2M \le N/2M≤N/2, the whole sequence can be deleted.

Hence the answer is

max⁡(0,2M−N),\max(0,2M-N),max(0,2M−N),

where MMM is the maximum frequency of any value.

The implementation only needs to count frequencies and find the maximum one.

The time complexity is O(N)O(N)O(N).

22
2
xxx
xxx
2M−N2M-N2M−N
33
3