#P10628. [JOI Open 2024] 图书馆 3 / Library 3

    ID: 12103 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>二分2024交互题Special Judge置换JOI(日本)

[JOI Open 2024] 图书馆 3 / Library 3

Background

Due to limitations of the Luogu judging system, in actual submission you do not need to include library3.h\texttt{library3.h}. 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 NN positions for placing books, numbered from left to right as 0N10 \sim N-1. Each position can hold only one book.
  • There are NN books on the shelf, numbered 0N10 \sim N-1.
  • A placement of the NN books is a way to place the NN books into the NN positions.
  • There exists a correct placement of the NN books, in which book BiB_i (0iN10 \le i \le N-1) is placed at position ii, and all BiB_i 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 xx be the leftmost book such that the position of xx is different from its position in the correct placement. Let yy be the book currently at the correct position of xx. Swap xx and yy.

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 50005\,000 queries.

You need to write a program. Given the bookshelf information, find the correct placement using at most 50005\,000 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 NN 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 a is an array of length NN, i.e., the placement to be queried. That is, in the specified placement, book a[i] (0iN10 \le i \le N-1) is placed at position ii.
    • 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 a must be NN. If this condition is not satisfied, your program will be judged Wrong Answer[1].
    • In the parameter, every element of array a must be between 00 and N1N-1. If this condition is not satisfied, your program will be judged Wrong Answer[2].
    • In the parameter, the elements of array a must be pairwise distinct. If this condition is not satisfied, your program will be judged Wrong Answer[3].
    • This function can be called at most 50005\,000 times. If the call limit is exceeded, your program will be judged Wrong Answer[4].
  • void answer(std::vector<int> b)
    Your program uses this function to report the correct placement.

    • The parameter b is an array of length NN, i.e., the correct placement. That is, in the correct placement, book b[i] (0iN10 \le i \le N-1) is placed at position ii.
    • In the parameter, the length of array b must be NN. If this condition is not satisfied, your program will be judged Wrong Answer[5].
    • In the parameter, every element of array b must be between 00 and N1N-1. If this condition is not satisfied, your program will be judged Wrong Answer[6].
    • In the parameter, the elements of array b must 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].

[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:

NN
B0B_0 B1B_1 \cdots BN1B_{N-1}

In the correct placement, book BiB_i (0iN10 \le i \le N-1) is placed at position ii.

Output Format

The sample grader outputs the following result:

  • If your program is judged correct, it outputs the number of calls to the query function. 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 00 and book 11. Then books 1,0,2,31,0,2,3 are placed at positions 0,1,2,30,1,2,3, respectively.
  • Swap the positions of book 11 and book 33. Then books 3,0,2,13,0,2,1 are placed at positions 0,1,2,30,1,2,3, respectively.
  • Swap the positions of book 33 and book 22. Then books 2,0,3,12,0,3,1 are placed at positions 0,1,2,30,1,2,3, respectively.

After 33 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

  • 2N5002 \le N \le 500.
  • 0BiN10 \le B_i \le N - 1 (0iN10 \le i \le N - 1).
  • BiBjB_i \neq B_j (0i<jN10 \le i < j \le N - 1).
  • All input numbers are integers.

Subtasks

  1. (22 points) N6N \le 6.
  2. (1919 points) N100N \le 100.
  3. (7979 points) No additional constraints.

Translated by Starrykiller from the English statement.

Translated by ChatGPT 5