Introduction
This is one of my favourite problems on Codeforces. I solved this problem when I had just started with Codeforces. Solving this problem during the live contest as a newbie, unaware of concepts like dynamic programming or binary search gave me a massive confidence boost. After the contest I found out that my solution was the shortest and simplest solution to the problem, more efficient than even the editorial.
Statement
The score of a sequence is defined as
where . In particular, the score of an empty sequence is 1.
For a sequence , let be the maximum score among all its subsequences. Its cost is defined as the maximum length of a subsequence with a score of .
You are given a non-decreasing sequence of integers of length . In other words, the condition
is satisfied. For each , find the cost of the sequence .
A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements.
Example
- The maximum score among the subsequences of is 1. The subsequences and (the empty sequence) are the only ones with this score. Thus, the cost of is 1.
- The maximum score among the subsequences of is 2. The only subsequence with this score is . Thus, the cost of is 1.
- The maximum score among the subsequences of is 3. The subsequences and are the only ones with this score. Thus, the cost of is 2.
Therefore, the answer to this case is , which are the costs of , and in this order.
Official Editorial (Suboptimal)
We first note that for a non-decreasing sequence
the maximum score for any subsequence of elements is achieved by taking the largest elements—that is, a suffix of the sequence.
Now, transform the sequence by dividing the th element from the right by , so it becomes
The score of a suffix in the original sequence equals the product of the corresponding suffix in this new sequence.
Since
and
we have
Thus, to maximize the product, we choose the longest suffix of the transformed sequence in which every element is at least . This length is defined as the cost of the sequence.
For each prefix
of a non-decreasing sequence
its cost is the maximum length of a suffix of
with all elements . We can compute this via binary search (determining each required fraction on the fly), leading to an overall complexity of per test case.
My Solution (Optimal)
We can solve the problem by computing the cost of every prefix of a non-decreasing sequence step by step. Recall that for a sequence
the cost is defined as the maximum length of a suffix of
such that each element is at least (which ensures that the score/product is maximized).
The core idea is to maintain a counter , representing the current cost (i.e. the maximum length we can achieve for the prefix). For each new element, we try to extend our valid suffix of the transformed sequence by checking whether the candidate element (which will become the smallest element in the new suffix) satisfies:
In a 0-indexed array, when processing a prefix of length , the candidate is —because the largest elements, which form our new suffix after adding the current element, start at index . If this condition holds, then dividing by its new denominator gives a value at least , so the valid suffix can be extended.
The following Python code implements this strategy:
for _ in range(int(input())):
n = int(input())
arr = [int(i) for i in input().split()]
c = 1
result = [c]
for i in range(1, n):
if arr[i - c] >= c + 1:
c += 1
result.append(c)
print(*result)
Code Breakdown:
- For each test case, we read the number and the non-decreasing sequence .
- We initialize the cost , since the first element provides a valid subsequence of length .
- For each subsequent index , we check if the candidate element meets the condition If true, the candidate is strong enough (after the division by ) to keep every element in the suffix at least , allowing us to increment .
- We append the current cost to the result for every prefix.
This approach efficiently computes each prefix’s cost in a single pass, giving a time complexity of per test case.
If you have any doubts or suggestions or just want to interact with me, use the comment section below (Refresh if comments don’t load)