#P15587. [KTSC 2026] 排序 / Sorting
[KTSC 2026] 排序 / Sorting
Background
Submission notes:
- Do not include any header files.
- 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>>); - Submit using .
Problem Description
This is an interactive problem. In this problem, the interaction library is adaptive.
Alice and Bob are playing a game. Alice has items, numbered . The value of item is a non-negative integer .
Alice knows the values of all items, but Bob only knows the number of items , 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 of such that:
- For any , .
To do this, Bob can ask Alice queries.
Query
- Bob provides a tree with nodes, where the nodes are numbered . The node weight of node is .
- 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 (which may be empty), such that no two nodes in are connected by an edge, and 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)
- : the number of items.
- Return an array 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 , describing the edges of the tree. Each element inthreadsrepresents an edge .threadsmust describe a tree.- Returns an integer array of size . If the chosen maximum independent set contains , then ; otherwise .
- If there are multiple solutions, Alice will return one arbitrarily. Note that if you pass the same
threadsarray multiple times within the same test case, the return value may differ.
- If there are multiple solutions, Alice will return one arbitrarily. Note that if you pass the same
- In each test case, this function can be called at most times.
Input Format
The input format of the sample grader is as follows:
- Line :
- Line :
The provided sample grader is only guaranteed to work properly when is an integer between and (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 : Suppose
sortingreturns an array of length , print . - Line : The number of calls to
ask_question, .
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
- .
- is a non-negative integer. Note that there is no upper bound on .
- In each test case, you can call
ask_questionat most times. - The interaction library is adaptive. In other words, is not fixed and may change depending on how
ask_questionis called. When answering, the interaction library guarantees that there exists an array consistent with all previous answers fromask_question. - In each test case, the interaction library uses at most seconds and of memory.
Subtasks
| ID | Score | Constraints |
|---|---|---|
| , there exists at most one such that | ||
| , | ||
| No additional constraints |
Scoring
Subtasks
If your answer is valid, you get full score.
Subtasks
If the answer is wrong, or the program terminates abnormally, you get points.
Otherwise, let be the maximum number of calls to ask_question within a single test case of this subtask, and compute :
| Condition | |
|---|---|
This subtask receives of its score.
Example
Consider and the array representing the values of Alice’s items is .
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 , the total value is , which is the maximum. Therefore, this call returns .
In the second call, the item sets Alice can choose (while satisfying the condition) are and . Therefore, this call returns or .
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 .
In the second call, this is not a valid call because the given graph is not a tree.
There are two valid integer arrays :
Therefore, the function must return one of these two arrays.
Translated by ChatGPT 5