#P15587. [KTSC 2026] 排序 / Sorting

    ID: 17537 远端评测题 5000ms 2048MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>交互题Special Judge2026KTSC(韩国)

[KTSC 2026] 排序 / Sorting

Background

Submission notes:

  1. Do not include any header files.
  2. Add the following at the beginning of the file:
    #include <vector>
    #include <array>
    std::vector<int> sorting(int);
    std::vector<int> ask_question(std::vector<std::array<int, 2>>);
    
  3. Submit using C++ 20/23\texttt{C++\,\red{20/23}}.

Problem Description

This is an interactive problem. In this problem, the interaction library is adaptive.

Alice and Bob are playing a game. Alice has NN items, numbered 0∼N−10\sim N-1. The value of item ii is a non-negative integer A[i]A[i].

Alice knows the values of all items, but Bob only knows the number of items NN, and that all item values are non-negative integers. Bob’s goal is to sort the items in non-decreasing order of value. In other words, Bob needs to find a permutation PP of 0∼N−10\sim N-1 such that:

  • For any 0≤i≤N−20\le i\le N-2, A[P[i]]≤A[P[i+1]]A[P[i]]\le A[P[i+1]].

To do this, Bob can ask Alice 10 00010\,000 queries.

Query

  1. Bob provides a tree with NN nodes, where the nodes are numbered 0∼N−10\sim N-1. The node weight of node ii is A[i]A[i].
  2. Alice selects any maximum independent set of this tree (it may be empty) and returns it to Bob. Then Alice clears this tree.
    • In other words, Alice chooses a set of nodes V⊆{0,1,…,N−1}V\subseteq \{0,1,\ldots,N-1\} (which may be empty), such that no two nodes in VV are connected by an edge, and ∑v∈VA[v]\sum_{v\in V} A[v] is maximized.

      If there are multiple valid sets, Alice will choose one arbitrarily.

Help Bob achieve the goal using as few queries as possible.

Implementation Details

This is a functional interactive problem. You do not need to, and should not, implement the main function.

You should implement the following function:

vector<int> sorting(int N)
  • NN: the number of items.
  • Return an array PP that sorts the item indices in non-decreasing order of value. If there are multiple solutions, you may return any of them.
  • This function is called exactly once.

You may call the following function:

vector<int> ask_question(vector<array<int, 2>> threads)
  • Represents one query from Bob to Alice.
  • threads: an array of pairs of size N−1N-1, describing the edges of the tree. Each element [a,b][a,b] in threads represents an edge (a,b)(a,b).
  • threads must describe a tree.
  • Returns an integer array CC of size NN. If the chosen maximum independent set contains ii, then C[i]=1C[i]=1; otherwise C[i]=0C[i]=0.
    • If there are multiple solutions, Alice will return one arbitrarily. Note that if you pass the same threads array multiple times within the same test case, the return value may differ.
  • In each test case, this function can be called at most 10 00010\,000 times.

Input Format

The input format of the sample grader is as follows:

  • Line 11: NN
  • Line 22: A[0]A[1]…A[N−1]A[0] A[1] \dots A[N - 1]

The provided sample grader is only guaranteed to work properly when A[i]A[i] is an integer between 00 and 10910^9 (inclusive).

Output Format

The sample grader prints the array returned by your code in the sorting function and the number of calls to ask_question in the following format:

  • Line 11: Suppose sorting returns an array PP of length MM, print P[0]P[1]…P[M−1]P[0] P[1] \dots P[M - 1].
  • Line 22: The number of calls to ask_question, QQ.

Note that the sample grader may be different from the grader used in the actual evaluation.

6
5 3 3 0 8 1

3 5 1 2 0 4
2

Hint

Constraints

  • 5≤N≤1 0005\le N\le 1\, 000.
  • A[i]A[i] is a non-negative integer. Note that there is no upper bound on A[i]A[i].
  • In each test case, you can call ask_question at most 10 00010\,000 times.
  • The interaction library is adaptive. In other words, AA is not fixed and may change depending on how ask_question is called. When answering, the interaction library guarantees that there exists an array AA consistent with all previous answers from ask_question.
  • In each test case, the interaction library uses at most 22 seconds and 16 MiB16\, \mathrm{MiB} of memory.

Subtasks

ID Score Constraints
11 7 7 N=5N=5
22 8 8 N≤100N \le 100
33 1010 ∀0≤i<N\forall 0\le i\lt N, there exists at most one ii such that A[i]>0A[i]\gt 0
44 3030 ∀0≤i<N2\forall 0\le i\lt \frac{N}{2}, A[i]=0A[i]=0
55 4545 No additional constraints

Scoring

Subtasks 1,21,2

If your answer is valid, you get full score.

Subtasks 3,4,53,4,5

If the answer is wrong, or the program terminates abnormally, you get 00 points.

Otherwise, let Qmax⁡Q_{\max} be the maximum number of calls to ask_question within a single test case of this subtask, and compute XX:

Condition X=X=
10 000<Qmax⁡10\, 000 \lt Q_{\max} 00
80<Qmax⁡≤10 00080 \lt Q_{\max}\le 10\, 000 90−35log⁡10(Qmax⁡80)90 - 35\log_{10}\left(\frac{Q_{\max}}{80}\right)
70<Qmax⁡≤8070\lt Q_{\max}\le 80 170−Qmax⁡170-Q_{\max}
Qmax⁡≤70Q_{\max} \le 70 100100

This subtask receives X%X\% of its score.

Example

Consider N=6N = 6 and the array AA representing the values of Alice’s items is [5,3,3,0,8,1][5, 3, 3, 0, 8, 1].

The grader first calls:

sorting(6)

Your code may interact as follows:

ask_question([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]])
ask_question([[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]])

In the first call, if Alice selects items 0,2,40, 2, 4, the total value is 5+3+8=165 + 3 + 8 = 16, which is the maximum. Therefore, this call returns [1,0,1,0,1,0][1, 0, 1, 0, 1, 0].

In the second call, the item sets Alice can choose (while satisfying the condition) are {1,2,4,5}\{1, 2, 4, 5\} and {1,2,3,4,5}\{1, 2, 3, 4, 5\}. Therefore, this call returns [0,1,1,0,1,1][0, 1, 1, 0, 1, 1] or [0,1,1,1,1,1][0, 1, 1, 1, 1, 1].

Consider the following interactions:

ask_question([[0, 1], [2, 3], [4, 5]])
ask_question([[0, 1], [1, 2], [2, 3], [3, 0], [4, 5]])

In the first call, this is not a valid call because the size of the threads array is not N−1N - 1.

In the second call, this is not a valid call because the given graph is not a tree.

There are two valid integer arrays PP:

  • [3,5,1,2,0,4][3, 5, 1, 2, 0, 4]
  • [3,5,2,1,0,4][3, 5, 2, 1, 0, 4]

Therefore, the function must return one of these two arrays.

Translated by ChatGPT 5