#P10628. [JOI Open 2024] 图书馆 3 / Library 3
[JOI Open 2024] 图书馆 3 / Library 3
Background
Due to limitations of the Luogu judging system, in actual submission you do not need to include . You need to add the following to your code:
void answer(std::vector<int>);
int query(std::vector<int>);
Please submit in C++ 20. Passing is not guaranteed if you use other standards.
Problem Description
In the blink of an eye, hundreds of years have passed, and JOI City has already become ruins. IOI-chan, an explorer, is exploring the former site of the library. From the exploration results, it is known that:
- In the library of JOI City, there is a horizontal bookshelf with positions for placing books, numbered from left to right as . Each position can hold only one book.
- There are books on the shelf, numbered .
- A placement of the books is a way to place the books into the positions.
- There exists a correct placement of the books, in which book () is placed at position , and all are pairwise distinct.
The placement of books keeps changing, and the library restores the books to the correct placement by repeatedly performing the following step:
Operation Let be the leftmost book such that the position of is different from its position in the correct placement. Let be the book currently at the correct position of . Swap and .
Although IOI-chan found many old books, she cannot know the correct placement. However, she found an old machine that can perform the operation above. If she specifies a placement and queries the machine, the machine will answer how many times the operation must be repeated to go from the current placement to the correct placement. IOI-chan wants to use this machine to obtain the correct placement. Since the machine is too old, she can make at most queries.
You need to write a program. Given the bookshelf information, find the correct placement using at most queries.
[Implementation Details]
This is an interactive problem. You only need to submit one file (library3.cpp).
You need to include library3.h in the file and implement the following function:
void solve(int N)
This function is called exactly once for each test case.- The parameter represents the number of books.
In library3.cpp, you can call the following functions:
-
int query(const std::vector<int> a)
IOI-chan uses this function to query the machine.- The parameter
ais an array of length , i.e., the placement to be queried. That is, in the specified placement, booka[i]() is placed at position . - The return value is an integer, i.e., how many times the operation must be repeated to restore the specified placement to the correct placement.
- In the parameter, the length of array
amust be . If this condition is not satisfied, your program will be judged Wrong Answer[1]. - In the parameter, every element of array
amust be between and . If this condition is not satisfied, your program will be judged Wrong Answer[2]. - In the parameter, the elements of array
amust be pairwise distinct. If this condition is not satisfied, your program will be judged Wrong Answer[3]. - This function can be called at most times. If the call limit is exceeded, your program will be judged Wrong Answer[4].
- The parameter
-
void answer(std::vector<int> b)
Your program uses this function to report the correct placement.- The parameter
bis an array of length , i.e., the correct placement. That is, in the correct placement, bookb[i]() is placed at position . - In the parameter, the length of array
bmust be . If this condition is not satisfied, your program will be judged Wrong Answer[5]. - In the parameter, every element of array
bmust be between and . If this condition is not satisfied, your program will be judged Wrong Answer[6]. - In the parameter, the elements of array
bmust be pairwise distinct. If this condition is not satisfied, your program will be judged Wrong Answer[7]. - If the placement you report is not the correct placement, your program will be judged Wrong Answer[8].
- This function must be called exactly once. If the call limit is exceeded, your program will be judged Wrong Answer[9]. If it is not called, your program will be judged Wrong Answer[10].
- The parameter
[Notes]
Your program may define other functions and may use global variables.
Your program must not use standard input/output streams, and must not read or write any other files in any form. However, your program may output debug information to the standard error stream.
[Compile and Run]
You can obtain the sample grader from the attachments. The sample grader is the file grader.cpp. Put grader.cpp, library3.cpp, and library3.h in the same directory, and compile your program using the following command. You may also run compile.sh to compile.
Compilation command:
g++ -std=gnu++20 -O2 -o grader grader.cpp library3.cpp
If compilation succeeds, an executable file grader will be generated. Note that the grader used in actual judging is different from the sample grader. The sample grader runs in a single process, reads input from standard input, and outputs results to standard output.
*Contest announcement: The sample grader is non-adaptive. Whether the grader used in actual judging is adaptive is unknown.
Input Format
The sample grader reads input in the following format:
In the correct placement, book () is placed at position .
Output Format
The sample grader outputs the following result:
- If your program is judged correct, it outputs the number of calls to the
queryfunction. For example:Accepted: 3000. - Otherwise, if your program satisfies any type of error, it outputs the error type as described in the statement. For example:
Wrong Answer[3].
If your program satisfies multiple error conditions at the same time, only one error will be output.
4
2 0 3 1
Hint
Below is one possible interaction process:
| Call | Return value | |
|---|---|---|
solve(4) |
||
query([0, 1, 2, 3]) |
3 |
|
query([1, 3, 0, 2]) |
2 |
|
query([3, 0, 1, 2]) |
||
query([2, 0, 3, 1]) |
0 |
|
answer([2, 0, 3, 1]) |
||
Consider calling query([0, 1, 2, 3]). The operations will be performed as follows:
- Swap the positions of book and book . Then books are placed at positions , respectively.
- Swap the positions of book and book . Then books are placed at positions , respectively.
- Swap the positions of book and book . Then books are placed at positions , respectively.
After operations, the specified placement is restored to the correct placement, so the return value is 3.
The sample satisfies the constraints of all subtasks.
Constraints
- .
- ().
- ().
- All input numbers are integers.
Subtasks
- ( points) .
- ( points) .
- ( points) No additional constraints.
Translated by Starrykiller from the English statement.
Translated by ChatGPT 5