Editorial for JOI '21 Open P2 - Financial Report
Submitting an official solution before solving the problem yourself is a bannable offence.
In this review, we explain a solution for each of 6 Subtasks. We first explain how to solve Subtask 1 by a
brute-force search algorithm whose time complexity is . Then we explain a solution to Subtask 2 by
Dynamic Programming whose time complexity is 
. An improvement of it gives a solution to Subtask 3
whose time complexity is 
.
Then we explain faster algorithm which is valid in some special circumstances such as Subtask 4 ()
and Subtask 5 (
). Finally, we explain full score solution whose time complexity is 
. It is an
improvement of the solution of Subtask 3 by data structures. for certain how to solve the task faster for
If you hurry to read a full score solution, please see "Full Score Solution" section.
Subtask 1 (
)
First, we consider a brute-force search algorithm to choose the data for the presentation. There are two
choices (Bitaro chooses it or does not choose it) for the data for each day. In total, there are 
choices to choose the data. The solution of this task is the maximum of the impression scores for these choices.
How can we list all of the  possible choices by a brute-force algorithm? There are several methods of
implementation.
- Search all of the 
possible choices by a nested loop. (It is an unusual method because the implementation is hard.)
 - Use a recursive function.
 - Use a brute-force bit search using binary digit numbers.
 
Here we explain the third method because the implementation is easy. The idea of brute-force bit search is to
associate an integer between  and 
, inclusive, to each of the 
 choices. Precisely, we associate it in the
following way.
Consider the binary digits of an integer
(
). If the digit in the
-th place is
, Bitaro chooses the
-th data. If the digit in the
-th place is
, Bitaro does not choose the
-th data.
We can accomplish a brute-force search of  possible choices by writing a for loop from 
 to 
.
For each 
, we can check whether it satisfies the condition or not and calculate the impression scores in 
time. The total time complexity is 
. We can calculate the answer under the constraints of Subtask 1.
Thus we can get 15 points. The time complexity will be the same if we use other implementation methods such
as recursive functions.
Subtask 2 (
)
A brute-force search algorithm is not efficient because if  is increased by 
, then the time complexity is
multiplied by 
. Obviously it cannot give an answer when 
. We shall use Dynamic Programming (DP)
to improve the algorithm. In the following, we consider an algorithm to choose the data in chronological order
and update the values in the DP table sequentially
: the maximum number of times of making record-high sales amounts under the assumption that the final chosen data is the data of the
-th day and the maximum sales amount at that day is
yen.
Let  be the state that 
 indicates. If Bitaro chooses the data of the 
-th day
when the state is 
, the state becomes 
. Thus we can update the values in
the DP table in the following way.
- In the beginning, all the values of 
are the initial value
.
 - Do the operations 2-3 from smaller values of 
.
 - If 
, the value of
becomes
. It means the first data chosen by Bitaro is the data of the
-th day.
 - For every 
(
), we update the value of
to be
.
 - The answer is the maximum of the final values of 
.
 
However, we cannot calculate these values efficiently because the value of  can be as large as 
.
Here we shall use the coordinate compression technique because the answer depends only on the relative
magnitude relationship between 
. We may consider all values are compressed into the range 
.
Then, we have 
, and we can finish the calculation of the DP table in 
 time.
Subtask 3 (
)
We shall speed up the DP algorithm as above. We are now calculating the values of the DP table
 sequentially. In order to speed up the calculation, we need to do one of the following:
- We shall speed up the calculation of each 
.
 - We shall reduce the total number of possible states of DP. Currently, the number of possible values of
is
.
 
Here we explain how to reduce the calculation in the second part. We shall perform DP calculation without
considering . Now, the reason why we need to consider 
 is to decide whether a record-high sales
amount is achieved or not. In order to eliminate this consideration, we shall change the state of DP only when
a record-high sales amount is achieved. Let us calculate the following DP table sequentially.
: the maximum number of times of making record-high sales amounts under the assumption that the final chosen data is the data of the
-th day and the maximum sales amount is achieved at that day.
We can calculate the values in the DP table as follows: if it is possible to change the state from  to
 (i.e., it is possible to achieve a maximum sales amount on the 
-th day after achieving a maximum
sales amount on the 
-th day), then we update the value of 
 to be 
. It is
possible to change the state as 
 if and only if the following condition is satisfied.
(Condition)
, and, for each of the
-th day, we can choose a data whose sales amount is at most
yen so that the difference of any two consecutive dates is at most
days.
In order to satisfy the above condition, the optimum strategy is to choose all of the data whose sales amount
is at most  yen. Hence we can rewrite this condition as follows, and can determine it easily.
(Condition)
, and, for each of the
-th day, there are no consecutive
days when the sales amount exceeds
yen.
It takes  time to obtain the answer if, when we calculate the value of 
, we individually determine
whether it is possible to change the state as 
 for each 
. We can calculate whether it is
possible to change the state for every pair 
 in advance. Then, we can obtain the answer in 
 time
in total. The answer of this task is the maximum of 
.
Method of precalculation (Part 1)
There are several methods of precalculation. Here is a method. First, we note that the condition "for each of
the -th day, there are no consecutive 
 days when the sales amount exceeds 
yen" becomes stronger if the value of 
 increases. Therefore, the condition is satisfied for a range of the form
.
For each , the value of 
 is "the first position among 
 where 
 consecutive values
exceed 
." (If such a position does not exist, then 
.) Thus, it can be calculated in 
 time for each
. After we calculate the values of 
, we can determine whether it is possible to change the states
easily; the change of states is possible if and only if "
 and 
" are satisfied. We
can calculate it for every pair 
 in advance in 
 time.
The method of calculating  is related to the full score solution.
Subtask 4 (
)
Let us recall the conditions. When , we need to choose the data so that the following two conditions
are satisfied.
- Bitaro will show the latest sales amount.
 - For any two consecutive data, the difference of the dates is at most 
day. In other words, Bitaro has to choose the data of consecutive days.
 
Thus Bitaro has to choose the data of the -th days (
). There are 
 possible choices.
But, if we calculate the impression score for each choice, the total time complexity becomes 
, and we
cannot solve this Subtask.
In order to solve it, we shall calculate the answer for  in this order. We consider an
algorithm using the data structure called "stack" to store the position where the maximum sales amount is
achieved.
- Perform the operations 1-3 for 
, in this order.
- Delete (pop) the last element until the stack becomes empty or the last element of the stack becomes
at least 
.
 - Add 
to the end of the stack.
 - At that time, the size of the stack is the impression score when 
.
 
 - Delete (pop) the last element until the stack becomes empty or the last element of the stack becomes
at least 
 - The answer is the maximum of the impression scores calculated above.
 

It may happen that it takes  time to perform an operation of type 2. But the total time complexity is
 because the values are added to the stack 
 times and the number of times of deleting elements in the
operation 2 is at most 
.
Subtask 5 (
)
When , we need to choose the data so that the following two conditions are satisfied.
- Bitaro will show the latest sales amount.
 - For any two consecutive data, the difference of the dates is at most 
days.
 
We can ignore the second condition because it is always satisfied for any choice of the data for presentation. Thus we shall consider the first condition only. Moreover, the impression score will never increase if we delete the date of the last day. Also, the impression score will remain the same if we delete all of the data where the maximum sales amount is achieved. Therefore, we only need to consider the data where the maximum sales amount is achieved. Hence we need to calculate the following.
The maximum number of data so that the sales amounts at the chosen days are increasing in chronological order.
This is a famous problem called "Longest Increasing Subsequence (LIS)." It is well-known that it can be
solved in  time. There are several algorithms to solve it. Here is one such algorithm.
- In the beginning, we set the values of 
to be
.
 - Perform the following operation for 
, in this order.
- Find the minimum 
satisfying
by a binary search technique, and update the value of
to be
.
 
 - Find the minimum 
 - Finally, the number of 
such that the value of
is different from
is the length of the Longest Increasing Subsequence (LIS).
 
Note that the sequence  calculated by the above algorithm is not necessarily an example of
Longest Increasing Subsequence (LIS).
Full Score Solution
Before, we calculate the DP table in chronological order (i.e., the order of the data ). However,
in the DP solution of Subtask 3, it is more convenient for us to calculate them in the reverse order (i.e., the order
of the data 
). Here we shall consider calculating the following DP table in the opposite order.
: the maximum number of times of making record-high sales amounts in the past (in chronological order) when we are choosing data opposite to the chronological order and the final chosen data is the data of the
-th day.
Note that we change the DP states in the opposite order. As in Subtask 3, the change of states 
is possible if and only if the following condition is satisfied.
(Condition)
, and, for each of the
-th day, there are no consecutive
days when the sales amount exceeds
yen.
As in the solution of Subtask 3, the value of  is "the first position among 
 where 
consecutive values exceed 
." (If such a position does not exist, then 
.) Then we can change the state
to be 
 in the range 
 satisfying 
. We can calculate the DP table by the
following simple formula. (If we calculate the table by this formula, the total time complexity becomes 
.)
We need to speed up this algorithm using a data structure. In order not to consider the condition ,
we shall calculate the values of 
 in decreasing order. We have the following algorithm.
- We set the values of 
to be
.
 - Perform the following operation in decreasing order of the value of 
(in increasing order of
if the values are the same).
- We set 
.
 
 - We set 
 - Finally, the answer is the maximum of 
.
 
It usually takes  time to calculate one 
. Since calculating the minimum in a range is RMQ
(Range Maximum Query), we can calculate it in 
 using a segment tree. Thus, once 
 are
calculated, we can obtain the answer in 
 time in total.
Method of precalculation (Part 2)
The remaining task is to calculate the values of . We can calculate it in 
 time by
the algorithm explained in Subtask 3. We need to speed up it. Recall that 
 is "the first position among
 where 
 consecutive values exceed 
." In order words, it is the first value 
where all of the values of 
 exceed 
.
Here we put . Then 
 is the minimum of 
 in the range 
 such
that 
 is satisfied. Hence we can calculate them by the following way.
- For 
, calculate
. For example, they can be calculated in
time if we use a RMQ segment tree.
 - Then 
is equal to the leftmost index among
where the value exceeds
. For example, it can be calculated in
time in total if we use a binary search technique on a RMQ segment tree.
 
Hence we can calculate the values of  in 
 time in total.
There are several ways to calculate the values of  in advance. Here we only explain one way.
Some contestants may solve this task by other methods. For example, in the operation 1, we can calculate
 in 
 time by a "sliding minimum technique." There are also completely different
methods. For example, we can calculate 
 in the increasing order of 
 using 
std::set.
Comments