#P8494. [IOI 2022] 最罕见的昆虫
[IOI 2022] 最罕见的昆虫
Background
This is an interactive problem.
You do not need and must not include the insects.h header file or the main function in your submitted program.
However, in your program you need to declare the following three functions:
void move_inside(int i);
void move_outside(int i);
int press_button();
For example, your program can look like this:
#include <bits/stdc++.h>
using namespace std;
void move_inside(int i);
void move_outside(int i);
int press_button();
int min_cardinality(int N) {
// Code Here
}
Background
Problem Description
There are insects around Pak Blangkon’s house, numbered from to . Each insect has a type, represented by an integer ID from to (including and ). Multiple insects may have the same type.
Suppose we group the insects by type. We define the cardinality of the most common insect type as the number of insects in the largest group. Similarly, the cardinality of the rarest insect type is the number of insects in the smallest group.
For example, suppose there are insects with types . In this case, the cardinality of the most common insect type is , because the groups of type and type both have the largest number of insects, and each group has insects. The cardinality of the rarest insect type is , because the groups of type , type , and type all have the smallest number of insects, and each group has insect.
Pak Blangkon does not know the types of these insects. He has a one-button machine that can provide information about insect types. Initially, the machine is empty. When using the machine, you can perform the following three operations:
- Put an insect into the machine.
- Take an insect out of the machine.
- Press the button on the machine.
Each operation can be performed at most times.
Whenever the button is pressed, the machine reports the cardinality of the most common insect type currently inside the machine.
Your task is to use the machine above to determine the cardinality of the rarest insect type among all insects around Pak Blangkon’s house. Also, in some subtasks, your score depends on the maximum number of times the machine performs a certain operation (see the Subtasks section for details).
Input Format
You need to implement the following function:
int min_cardinality(int N)
- : the number of insects.
- This function should return the cardinality of the rarest insect type among all insects around Pak Blangkon’s house.
- This function is called exactly once.
This function may call the following functions:
void move_inside(int i)
- : the ID of the insect to be put into the machine. The value of is between and (including and ).
- If the insect is already inside the machine, the call will not change the set of insects inside the machine. However, the call is still counted toward the number of such operations.
- This function can be called at most times.
void move_outside(int i)
- : the ID of the insect to be taken out of the machine. The value of is between and (including and ).
- If the insect is already outside the machine, the call will not change the set of insects inside the machine. However, the call is still counted toward the number of such operations.
- This function can be called at most times.
int press_button()
- This function returns the cardinality of the most common insect type inside the machine.
- This function can be called at most times.
- The grader is not adaptive. That is, the types of all insects are fixed before
min_cardinalityis called.
Output Format
Consider a scenario where there are insects with types .
The function min_cardinality is called as follows:
min_cardinality(6)
This function calls move_inside, move_outside, and press_button in the following order.
| Function call | Return value | Insects in the machine | Insect types in the machine |
|---|---|---|---|
move_inside(0) |
|||
press_button() |
|||
move_inside(1) |
|||
press_button() |
|||
move_inside(3) |
|||
press_button() |
|||
move_inside(2) |
|||
move_inside(4) |
|||
move_inside(5) |
|||
press_button() |
|||
move_inside(5) |
|||
press_button() |
|||
move_outside(5) |
|||
press_button() |
At this point, there is enough information to conclude that the cardinality of the rarest insect type is .
Therefore, the function min_cardinality should return .
In this example, move_inside is called times, move_outside is called time, and press_button is called times.
Hint
Constraints
- .
Subtasks
- (10 points) .
- (15 points) .
- (75 points) no additional constraints.
If, on some test case, the number of calls to move_inside, move_outside, or press_button does not satisfy the constraints given in “Implementation details”, or the return value of min_cardinality is incorrect, then your score for that subtask is .
Let be the maximum of the following three values: the number of calls to move_inside, the number of calls to move_outside, and the number of calls to press_button.
In Subtask 3, you may receive partial score. Let be the maximum value of over all test cases in this subtask. Your score for this subtask will be calculated according to the following table:
| Condition | Score |
|---|---|
(CMS reports “Output isn’t correct”) |
|
Example grader
Let be an integer array of length , where is the type of insect with ID .
The example grader reads input in the following format:
- Line : .
- Line : .
If the example grader detects illegal behavior, it will output Protocol Violation: <MSG>, where <MSG> is one of the following:
invalid parameter: in a call tomove_insideormove_outside, the value of parameter is not in the range from to (including and ).too many calls: the number of calls to at least one ofmove_inside,move_outside, andpress_buttonexceeds .
Otherwise, the example grader outputs in the following format:
- Line : the return value of
min_cardinality. - Line : .
Notes
In the statement, the function interfaces use general type names void, bool, int, int[] (array), and union(bool, int[]).
In C++, the grader will use appropriate data types or implementations, as shown in the following tables:
void |
bool |
int |
int[] |
|---|---|---|---|
void |
bool |
int |
std::vector<int> |
union(bool, int[]) |
Length of array a |
|---|---|
std::variant<bool, std::vector<int>> |
a.size() |
In C++, std::variant is defined in the <variant> header.
A function with return type std::variant<bool, std::vector<int>> can return either a bool or a std::vector<int>.
The following sample code shows three functions that return a std::variant, and they all work correctly:
std::variant<bool, std::vector<int>> foo(int N) {
return N % 2 == 0;
}
std::variant<bool, std::vector<int>> goo(int N) {
return std::vector<int>(N, 0);
}
std::variant<bool, std::vector<int>> hoo(int N) {
if (N % 2 == 0) {
return false;
}
return std::vector<int>(N, 0);
}
Translated by ChatGPT 5