#P17273. [eJOI 2026] Automata
[eJOI 2026] Automata
Problem Description
There is a field consisting of cells in a row, numbered to from left to right. Each cell has a distinct height: the height of cell is , and the sequence is a permutation of the numbers .
Two cells with indices and are called close if and only if . In particular, every cell is close to itself.
A robot stands on the field, initially placed at some cell. The robot accepts commands of the following two types:
MAX: among all cells close to the robot's current cell, its next position is the unique cell with the maximum height;MIN: among all cells close to the robot's current cell, its next position is the unique cell with the minimum height.
Since a cell is close to itself, a command may leave the robot at the same cell.
A program is a finite sequence of commands, where every command is either MAX or MIN. If the robot starts at cell and follows program , it ends at a uniquely determined cell, denoted by .
You will be asked queries. The -th query gives a set of starting cells . The robot will be placed at one of them, but which one is not known in advance. For each query, determine whether there is a program that moves the robot to the same final cell regardless of the chosen starting position.
Formally, determine whether there exists a program such that
$\operatorname{result}(S,x_0)=\operatorname{result}(S,x_1)=\cdots=\operatorname{result}(S,x_{K_i-1}).$
The permutation is the same for all queries.
You do not need to construct such a program; only report whether one exists.
Implementation details
Implement the following two functions:
void initialize(std::vector<int> p)
- : a permutation of the numbers from to .
bool exists_program(std::vector<int> x)
- : the cells for one query, given in strictly increasing order.
initialize is called exactly once, before any calls to exists_program.
exists_program is called times, once for each query. It must return true if there exists a program after which the robot ends at the same cell no matter which given cell it started from, and false otherwise.
Input Format
Input format:
- line : two integers and ;
- line : integers , where is the height of cell ;
- line : an integer , followed by integers describing the -th query.
Output Format
Output format:
- line : a binary string of length whose -th character is
1if the answer to query istrue, and0otherwise.
3 3
0 2 1
2 0 2
3 0 1 2
2 1 2
111
7 3
0 4 2 1 3 5 6
3 0 1 3
3 1 3 4
2 3 6
001
Hint
Explanation of example 1
Here . For the second query, the robot may start at cell , , or . Consider the program .
- Starting at cell , the close cells are and . Since , the robot moves to cell .
- Starting at cell , the close cells are , , and . Since and , the robot stays at cell .
- Starting at cell , the close cells are and . Since , the robot moves to cell .
Thus a program exists that ends at cell from all three starting cells, so the answer is true. Other valid programs include , $[\texttt{MIN},\texttt{MIN},\texttt{MAX},\texttt{MIN},\texttt{MAX},\texttt{MIN}]$, and .
Explanation of example 2
Here .
For the first two queries, no program can make the robot end at the same cell from every given starting cell, so both answers are false.
For the last query, the robot may start at cell or . Consider .
- Starting at cell , the close cells are , , and . Since and , the robot moves to cell . Applying the next two commands similarly, it ends at cell .
- Starting at cell , it remains at cell throughout.
Therefore the answer is true. The program $[\texttt{MIN},\texttt{MAX},\texttt{MAX},\texttt{MAX}]$ also works. In contrast, ends at cell from cell , but at cell from cell .
Constraints
- is a permutation of
- for every query
- The sum of over all queries does not exceed
Subtasks
| Subtask | Points | Additional constraints | |||
|---|---|---|---|---|---|
| 0 | - | The examples. | |||
| 1 | 3 | If the answer is positive, a valid program using exactly command exists. | |||
| 2 | 7 | If the answer is positive, a valid program using at most commands exists. | |||
| 3 | 9 | - | |||
| 4 | 17 | ||||
| 5 | 7 | for every query. | |||
| 6 | 8 | - | |||
| 7 | 13 | There exist such that $p_0<p_1<\cdots<p_a>p_{a+1}>\cdots>p_b<p_{b+1}<\cdots<p_{N-1}$. | |||
| 8 | if is even; if is odd. | ||||
| 9 | 23 | - | |||