#P6837. [IOI 2020] 数蘑菇

[IOI 2020] 数蘑菇

Description

Mushroom expert Andrew is studying local mushrooms in Singapore.

As part of the study, Andrew collected nn mushrooms, numbered from 00 to n1n-1. Each mushroom belongs to one of two species, called AA or BB.

Andrew knows that mushroom 00 belongs to species AA, but since the two species look very similar, he does not know whether mushrooms 11 to n1n-1 are AA or BB.

Fortunately, Andrew has a machine in his lab that can help. To use the machine, you put two or more mushrooms into the machine and arrange them in a line (in any order), then turn on the machine. The machine will compute the number of adjacent pairs of mushrooms that are not of the same species. For example, if you put mushrooms of species [A,B,B,A]\left[A, B, B, A\right] (in this order) into the machine, the result should be 22.

However, because operating the machine is very expensive, it can only be used a limited number of times. Moreover, across all uses of the machine, the total number of mushrooms placed into the machine cannot exceed 10510^5. Use this machine to help Andrew count how many mushrooms of species AA he collected.

Implementation details

You need to implement the following function:

int count_mushrooms(int n)
  • n: the number of mushrooms Andrew collected.
  • This function will be called exactly once, and it should return the number of mushrooms of species AA.

The above function may call the following function:

int use_machine(vector<int> x)
  • x: an array of length between 22 and nn (inclusive), giving in order the indices of the mushrooms placed into the machine.
  • The elements of x must be distinct integers between 00 and n1n-1 (inclusive).
  • Suppose the length of x is dd. Then this function returns the number of indices jj such that 0jd20 \le j \le d-2 and x[j]x[j] and x[j+1]x[j+1] belong to different species.
  • This function can be called at most 2×1042 \times 10^4 times.
  • Across all calls to use_machine, the sum of the lengths of all arrays xx passed to use_machine must not exceed 10510^5.

Hint

Sample explanation

Example 1

Consider the following scenario: there are 33 mushrooms, with species in order [A,B,B]\left[A,B,B\right]. The function count_mushrooms is called in the following way:

count_mushrooms(3)

This function can call use_machine([0, 1, 2]), which in this scenario returns 11. The function then calls use_machine([2, 1]), which returns 00.

At this point, there is enough information to conclude that there is only 11 mushroom of species AA. Therefore, count_mushrooms should return 11.

Example 2

Consider the following example: there are 44 mushrooms, with species in order [A,B,A,A]\left[A,B,A,A\right]. The function count_mushrooms is called as follows:

count_mushrooms(4)

This function can call use_machine([0, 2, 1, 3]), which returns 22. It then calls use_machine([1,2]), which returns 11.

At this point, there is enough information to conclude that there are 33 mushrooms of species AA. Therefore, count_mushrooms should return 33.

Constraints

  • 2n2×1042 \le n \le 2 \times 10^4

Scoring

For all test cases, if your calls to use_machine do not satisfy the requirements described above, or if the return value of count_mushrooms is not correct, your score will be 00. Otherwise, let QQ be the maximum number of calls to use_machine among all test cases. Then your score will be computed according to the following table:

Condition Score
2×104Q2 \times 10^4 \le Q 00
10010<Q2×10410010 < Q \le 2 \times 10^4 1010
904<Q10010904 < Q \le 10010 2525
226<Q904226 < Q \le 904 226Q100\dfrac{226}{Q} \cdot 100
Q226Q \le 226 100100

On some test cases, the behavior of the grader is adaptive. That is, in these test cases, the grader does not have a fixed sequence of mushroom species. Instead, the answers given by the grader may depend on previous calls to use_machine.

However, it is guaranteed that the answers given by the grader satisfy the following: after each interaction, there exists at least one sequence of mushroom species that is consistent with all answers given so far.

Example grader

The example grader reads an integer array ss, which gives the species of the mushrooms. For all 0in10 \le i \le n-1, s[i]=0s[i]=0 means mushroom ii is of species AA, and s[i]=1s[i]=1 means mushroom ii is of species BB. The example grader reads input in the following format:

Line 11: nn
Line 22: s[0] s[1]  s[n1]s[0]\ s[1]\ \ldots\ s[n-1]

The output of the example grader is in the following format:

Line 11: the return value of count_mushrooms.
Line 22: the number of calls to use_machine.

Note that the example grader is not adaptive.

Translated by ChatGPT 5