QOJ.ac

QOJ

时间限制: 1.000000 s 内存限制: 256 MB 总分: 100

#16551. Storage None

统计

This is an interactive problem. Note the unusual memory limit for this problem.

A permutation $p$ is a sequence containing every integer from $1$ to $n$ exactly once.

For a permutation $p = (p_1, p_2, \dots, p_n)$, its inverse permutation $q = (q_1, q_2, \dots, q_n)$ is defined as: if $p_i = j$, then $q_j = i$.

In this problem, you are given a permutation of $1$ to $n$. Your task is to calculate its inverse permutation.

Implementation Details

Unlike standard problems, you do not need to write a complete program (i.e., you do not need to implement a main function). Instead, you need to include the header file perm.h in your C/C++ source file and implement the following function:

void perm_work(int n);

The grader will call your perm_work function exactly once. The parameter n is the size of the permutation.

You cannot directly access the array storing the permutation. You need to interact with the permutation using the following functions provided by the grader:

  • int perm_get(int x): Get the value of the $x$-th element in the permutation, i.e., $p_x$. Here $x$ is a 1-based index, satisfying $1 \le x \le n$. If $x$ is out of range, the function will return $-1$.
  • void perm_set(int x, int y): Set the value of the $x$-th element in the permutation to $y$, i.e., set $p_x = y$. Here $x$ is a 1-based index. You must ensure $1 \le x, y \le n$, otherwise the function will have no effect.

Your perm_work function should modify the permutation in-place to become its own inverse by calling perm_get and perm_set.

Specifically, the first line of your submitted program may contain a comment in the form /* stack = S */ to specify the size of the program stack. S should be an integer representing the size of the program stack in bytes. If not specified, the grader will use a default value of $128$ bytes.

Constraints

The total linear memory your program can use is only 256 bytes. You can think of this as the entire memory space that your C/C++ code can directly access. This memory is divided into the following parts:

  • Program Stack: $S$ bytes. This memory is used to store local variables, return addresses, etc., during function calls. For example, int i; declared in your perm_work function will occupy this space. This is the same concept as the stack in a standard C/C++ program.
  • Static Space: $256 - 16 - S$ bytes. This space is used to store all global variables, static variables, and constants (such as string literals) in the program. All variables defined outside of functions will occupy this space.
  • System Reserved: $16$ bytes.

In addition to the linear memory mentioned above, the WebAssembly runtime itself requires an independent execution stack (Runtime Stack) to manage the program's execution flow. This stack is not in linear memory and cannot be accessed directly by the contestant. Its main purpose is to handle function calls at the WebAssembly instruction level.

  • Execution Stack: In this problem, its size is set to $1024$ bytes. For most programs, you do not need to worry about the usage of the execution stack. You mainly need to be concerned about whether the "Program Stack" overflows. However, for extremely deep function recursion, it is possible to exhaust the execution stack space, leading to a runtime error.

To ensure the program can be compiled and run under strict memory limits, there are some special restrictions:

  • Standard Library Prohibited: You cannot use any C/C++ standard library.
  • Certain C++ Features Disabled: For C++ contestants, disabling the standard library also means that new and delete operators, as well as exception handling, cannot be used.

Evaluation

Your C/C++ source code will be compiled into a format called WebAssembly (WASM). The grader will execute this WASM module and interact with it. During evaluation, only the WASM Ticks and memory of the program you wrote will be calculated; the WASM Ticks and memory of the interaction library will not be included.

During evaluation, time (in milliseconds) is displayed as the number of WASM Ticks divided by $10^6$ (rounded down). The actual space of 1 byte is displayed as 1 kb (the "kb" on UOJ is actually KiB, so it is displayed with a $1024$ multiplier).

Testing

For the convenience of contestants, two testing methods are provided:

You can use the "Custom Test" feature to test your code. The input format for the custom test is: the first line contains an integer $n$, and the next line contains $n$ integers $p_1, p_2, \dots, p_n$. The output will be the modified permutation. The test will follow the same process as the actual evaluation, except it will not check the correctness of the answer.

You can also compile and run your program locally. Download the attachment package for this problem, which contains two files: perm.h and user_grader.c. user_grader.c is a program that complies with both C and C++ standards. Place these two files in the same directory as your code, compile using <gcc | g++> your_code.c user_grader.c -o prog, then run ./prog (or prog.exe), and input the data according to the input format used in the "Custom Test" mentioned above. Note that you cannot obtain the WASM Tick count or accurate memory usage when testing with this method.

Constraints

This problem uses subtask evaluation; you only receive points for a subtask if you pass all test cases within that subtask.

  • Subtask 1 (10 points): $n \le 10$.
  • Subtask 2 (50 points): $n \le 10^4$.
  • Subtask 3 (40 points): No special constraints.

$1 \le n \le 3 \times 10^5$.

  • WASM Tick Limit: $10^{10}$ ticks.
  • Memory Limit: $256$ bytes.

Compilation Parameters

Your code will be compiled using the following key parameters:

  • -nostdlib, -nostdinc, -ffreestanding: Disable standard library.
  • -fno-exceptions: Disable C++ exception handling.
  • -O0: Disable optimization.
  • -Wl,-z,stack-size=S: Set stack size to $S$ bytes.
  • -Wl,--max-memory=256: Set total available linear memory to $256$ bytes.
  • -Wl,--export=perm_work: Make your perm_work function visible to the grader.
  • -Wl,--no-entry: Indicate that the program has no main entry point.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.