#P15652. [省选联考 2026] 排列游戏

    ID: 17715 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>各省省选交互题Special JudgeO2优化2026Ad-hoc

[省选联考 2026] 排列游戏

Background

This is an interactive problem.

Submission notes:

  1. Do not include the header file perm.h.
  2. Paste the following at the top of your file:
    #include <vector>
    void init(int, int);
    std::vector<int> perm(int);
    int query(int, int);
    
  3. Submit using C++ 17 / 20.

Problem Description

Xiao H and Xiao L are playing a game of guessing a permutation.

Xiao H has a permutation p=[p0,p1,…,pn−1]p = [p_0, p_1, \dots, p_{n-1}] of 0∼n−10 \sim n-1. Now Xiao L knows the length nn of the permutation, and he wants to guess this permutation pp through a special kind of queries. Specifically, Xiao L can ask Xiao H queries of the following form:

  • Given non-negative integers l,rl, r satisfying 0≤l≤r≤n−10 \le l \le r \le n-1, find the smallest non-negative integer that does not appear in pl,…,prp_l, \dots, p_r.

However, Xiao H and Xiao L found that even with infinitely many queries, sometimes it is still impossible to uniquely determine the permutation pp. So they agree on the following: suppose Xiao H’s answer is pp, and Xiao L’s guessed permutation is qq. If for any 0≤l≤r≤n−10 \le l \le r \le n-1, the smallest non-negative integer missing from the interval pl,…,prp_l, \dots, p_r is always equal to the smallest non-negative integer missing from the interval ql,…,qrq_l, \dots, q_r, then Xiao L’s guess is considered correct.

To make the game harder, Xiao H limits the number of queries Xiao L can make. You need to help Xiao L guess Xiao H’s permutation.

Implementation Details

Contestants do not need to, and should not, implement the main function.

Contestants need to make sure the submitted program includes the header file perm.h, i.e., add the following at the beginning of the program:

#include "perm.h"

In the submitted source file perm.cpp, contestants need to implement the following two functions:

void init(int c, int t);
  • c,tc, t denote the test point ID and the number of testdata groups, respectively. c=0c = 0 means this test point is the sample.
  • For each test point, this function will be called by the interactive library exactly once when the program starts running.
std::vector<int> perm(int n);
  • nn is the length of the permutation.
  • This function should return a permutation of 0∼n−10 \sim n-1, representing Xiao L’s guess.
  • For each test point, this function will be called by the interactive library exactly tt times.

Contestants can make one query by calling the following function:

int query(int l, int r);
  • l,rl, r specify the query interval. Contestants must ensure 0≤l≤r≤n−10 \le l \le r \le n-1.
  • This function returns the smallest non-negative integer that does not appear in pl,…,prp_l, \dots, p_r.
  • Contestants must ensure that each time the interactive library calls perm, the number of calls to this function does not exceed 6×1056 \times 10^5.

Note: In all cases, the interactive library used in the final tests will take no more than 0.10.1 seconds to run, and it uses a fixed amount of memory, which is no more than 6464 MiB.

How to Run the Tester

grader.cpp in the problem directory is a reference implementation of the interactive library. The interactive library used in the final tests is different from this reference implementation, so your solution should not rely on the library implementation.

You can compile an executable in the problem directory using the following command:

g++ grader.cpp perm.cpp -o perm -std=gnu++14 -O2 -static

Input Format

For the compiled executable:

  • The executable will read input from standard input in the following format:
    • The first line contains two non-negative integers c,tc, t, which are the test point ID and the number of testdata groups.
    • Then follow tt testdata groups. For each testdata group:
      • The first line contains a positive integer nn, the length of the permutation.
      • The second line contains nn non-negative integers p0,p1,…,pn−1p_0, p_1, \dots, p_{n-1}, which is Xiao H’s permutation.

Output Format

  • The executable will output to standard output in the following format:
    • For each testdata group:
      • The first line contains a string indicating the result. Specifically,
        • Correct means the contestant’s returned result is correct;
        • Wrong answer means the contestant’s returned result is incorrect;
        • Invalid operation means the contestant’s call to query is invalid.
      • If the result is Correct, then the second line contains a non-negative integer, which is the maximum number of calls to query among all testdata groups.
0 1
6
4 2 3 5 0 1
Correct.
4

Hint

Sample 1 Explanation

This sample contains one testdata group.

For the first testdata group, Xiao H’s permutation is p=[4,2,3,5,0,1]p = [4, 2, 3, 5, 0, 1].

Here is one possible interaction process:

  • Call query(0, 3), and the interactive library returns the smallest non-negative integer missing from 4,2,3,54, 2, 3, 5, which is 00.
  • Call query(3, 4), and the interactive library returns the smallest non-negative integer missing from 5,05, 0, which is 11.
  • Call query(1, 5), and the interactive library returns the smallest non-negative integer missing from 2,3,5,0,12, 3, 5, 0, 1, which is 44.
  • Call query(3, 5), and the interactive library returns the smallest non-negative integer missing from 5,0,15, 0, 1, which is 22.
  • Return q=[4,2,5,3,0,1]q = [4, 2, 5, 3, 0, 1].

It can be proven that the permutation qq is considered correct.

Sample 2

See perm/perm2.in and perm/perm2.ans in the contestant directory.

This sample satisfies the constraints of test points 4∼84 \sim 8.

Notes on Provided Files

In this problem directory:

  1. grader.cpp is the provided reference implementation of the interactive library.
  2. perm.h is the header file; contestants do not need to care about its specific contents.
  3. template_perm.cpp is the provided sample code, which contestants can refer to and use to implement their own code.

Contestants should back up all provided files. During final evaluation, only perm.cpp in this problem directory will be tested. Any changes to files other than this program will not affect the evaluation results.

Constraints

For all testdata:

  • t=10t = 10.
  • 2≤n≤3×1042 \le n \le 3 \times 10^4.
  • For all 0≤i≤n−10 \le i \le n-1, we have 0≤pi≤n−10 \le p_i \le n-1, and pp is a permutation of 0∼n−10 \sim n-1.

::cute-table{tuack}

Test Point ID n=n = Special Property
1∼31 \sim 3 1010 None
4∼84 \sim 8 10210^2
9,109, 10 3×1043 \times 10^4 A
11,1211, 12 ^ B
13,1413, 14 C
15∼2015 \sim 20 None
  • Special property A: p0=0p_0 = 0.
  • Special property B: There exists a non-negative integer k∈[0,n−1]k \in [0, n-1] such that p0,…,pkp_0, \dots, p_k is monotonically decreasing, and pk,…,pn−1p_k, \dots, p_{n-1} is monotonically increasing.
  • Special property C: pp is generated independently and uniformly at random among all permutations of 0∼n−10 \sim n-1.

Scoring

Note:

  • Contestants must not obtain internal information from the interactive library by illegal means, such as directly interacting with standard input/output streams. Such behavior will be considered cheating.
  • The interactive library used in the final evaluation is different from the sample interactive library.

This problem will first be subject to the same limits as traditional problems, e.g., compilation errors will cause the whole problem to score 00 points, runtime errors, time limit exceeded, memory limit exceeded, etc. will cause the corresponding test points to score 00 points. Contestants may only access variables they define and variables provided by the interactive library; attempting to access other address spaces may cause compilation errors or runtime errors.

Each time the perm function is called, if the returned permutation is not considered correct, or if the call to query is invalid, or if the number of calls to query exceeds 6×1056 \times 10^5, then the corresponding test point scores 00 points.

On top of the above conditions:

  • For test points 1∼31 \sim 3, the program gets full score if and only if, each time perm is called, the number of calls to query does not exceed 10210^2.
  • For test points 4∼84 \sim 8, let mm be the maximum number of queries per call to perm. The program will receive 5⋅f(m)5 \cdot f(m) points, where ff is computed as follows:

::cute-table{tuack} | mm | f(m)=f(m) = | |:-:|:-:| | m≤100m \le 100 | 11 | | 100<m≤200100 < m \le 200 | 1−m−100501 - \dfrac{\sqrt{m - 100}}{50} | | 200<m≤4950200 < m \le 4950 | 0.8−m−2001700.8 - \dfrac{\sqrt{m - 200}}{170} | | m>4950m > 4950 | 00 |

  • For test points 9∼209 \sim 20, let mm be the maximum number of queries per call to perm. The program will receive 5⋅g(m)5 \cdot g(m) points, where gg is computed as follows:

::cute-table{tuack} | mm | g(m)=g(m) = | |:-:|:-:| | m≤30000m \le 30000 | 11 | | 30000<m≤3001530000 < m \le 30015 | 1−7(m−30000)5(5m−149991)1 - \dfrac{7(m - 30000)}{5(5m - 149991)} | | 30015<m≤6000030015 < m \le 60000 | 0.75−m−300157000.75 - \dfrac{\sqrt{m - 30015}}{700} | | m>60000m > 60000 | 0.5−m−6000025000.5 - \dfrac{\sqrt{m - 60000}}{2500} |

Translated by ChatGPT 5