inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules:

  * If there are no occupied seats in the bus, a passenger can sit in any free seat;   * Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index $i$ ($1 \le i \le n$) only if at least one of the seats with indices $i-1$ or $i+1$ is occupied. 

Today, $n$ passengers boarded the bus. The array $a$ chronologically records the seat numbers they occupied. That is, $a_1$ contains the seat number where the first passenger sat, $a_2$ — the seat number where the second passenger sat, and so on.

You know the contents of the array $a$. Determine whether all passengers followed the recommendations.

For example, if $n = 5$, and $a$ = [$5, 4, 2, 1, 3$], then the recommendations were not followed, as the $3$-rd passenger sat in seat number $2$, while the neighboring seats with numbers $1$ and $3$ were free.

Input Format:
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The following describes the input test cases.

The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of seats in the bus and the number of passengers who boarded the bus.

The second line of each test case contains $n$ distinct integers $a_i$ ($1 \le a_i \le n$) — the seats that the passengers occupied in chronological order.

It is guaranteed that the sum of $n$ values across all test cases does not exceed $2 \cdot 10^5$, and that no passenger sits in an already occupied seat.

Output Format:
For each test case, output on a separate line:

  * "YES", if all passengers followed the recommendations;    * "NO" otherwise. 

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
Kristina has an array $a$, called a template, consisting of $n$ integers. She also has $m$ strings, each consisting only of lowercase Latin letters. The strings are numbered from $1$ to $m$. She wants to check which strings match the template.

A string $s$ is considered to match the template if all of the following conditions are simultaneously satisfied:

  * The length of the string $s$ is equal to the number of elements in the array $a$.   * The same numbers from $a$ correspond to the same symbols from $s$. So, if $a_i = a_j$, then $s_i = s_j$ for ($1 \le i, j \le n$).   * The same symbols from $s$ correspond to the same numbers from $a$. So, if $s_i = s_j$, then $a_i = a_j$ for ($1 \le i, j \le n$).

In other words, there must be a one-to-one correspondence between the characters of the string and the elements of the array.

For example, if $a$ = [$3, 5, 2, 1, 3$], then the string "abfda" matches the template, while the string "afbfa" does not, since the character "f" corresponds to both numbers $1$ and $5$.

Input Format:
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The following descriptions are for the test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in the array $a$.

The second line of each test case contains exactly $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$) — the elements of the array $a$.

The third line of each test case contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of strings to check for template matching.

Following are $m$ strings, each containing a non-empty string $s_j$ ($1 \le |s_j| \le 2 \cdot 10^5$), consisting of lowercase Latin letters.

It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$, and that the sum of the lengths of all strings does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output $m$ lines. On the $i$-th line ($1 \le i \le m$) output:

  * "YES", if the string with index $i$ matches the template;    * "NO" otherwise. 

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['data structures', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'.

Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.

In one operation, you can choose two indices $l$ and $r$ ($1 \le l < r \le n$) such that $s_l$ = 'L' and $s_r$ = 'R' and do the following:

  * add $a_l + a_{l + 1} + \dots + a_{r - 1} + a_r$ points to the current score;    * replace $s_i$ with '.' for all $l \le i \le r$, meaning you can no longer choose these indices. 

For example, consider the following strip:

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   L| R| L| L| L| R      You can first choose $l = 1$, $r = 2$ and add $3 + 5 = 8$ to your score.

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   .| .| L| L| L| R      Then choose $l = 3$, $r = 6$ and add $1 + 4 + 3 + 2 = 10$ to your score.

$3$| $5$| $1$| $4$| $3$| $2$   ---|---|---|---|---|---   .| .| .| .| .| .      As a result, it is impossible to perform another operation, and the final score is $18$.

What is the maximum score that can be achieved?

Input Format:
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the strip.

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) — the numbers written on the strip.

The third line of each test case contains a string $s$ of $n$ characters 'L' and 'R'.

It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output one integer — the maximum possible number of points that can be scored.

Tags:
['greedy', 'implementation', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
You really love gorillas, so you decided to organize a photoshoot for them. Gorillas live in the jungle. The jungle is represented as a grid of $n$ rows and $m$ columns. $w$ gorillas agreed to participate in the photoshoot, and the gorilla with index $i$ ($1 \le i \le w$) has a height of $a_i$. You want to place all the gorillas in the cells of the grid such that there is no more than one gorilla in each cell.

The spectacle of the arrangement is equal to the sum of the spectacles of all sub-squares of the grid with a side length of $k$.

The spectacle of a sub-square is equal to the sum of the heights of the gorillas in it.

From all suitable arrangements, choose the arrangement with the maximum spectacle.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^3$) — the number of test cases.

The descriptions of the test cases follow.

The first line contains integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le n \cdot m \le 2 \cdot 10^5$, $1 \le k \le \min(n, m)$) — the dimensions of the grid and the side length of the square.

The second line contains an integer $w$ ($1 \le w \le n \cdot m$) — the number of gorillas.

The third line contains $w$ integers $a_1, a_2, \ldots, a_w$ ($1 \le a_i \le 10^9$) — the heights of the gorillas.

It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $2 \cdot 10^5$. The same guarantee applies to $w$.

Output Format:
For each test case, output a single integer — the maximum spectacle of a suitable arrangement.

Tags:
['combinatorics', 'data structures', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
You have $n$ rectangles, the $i$-th of which has a width of $a_i$ and a height of $b_i$.

You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.

Each time you completely color any row or column, you earn $1$ point. Your task is to score at least $k$ points with as few operations as possible.

Suppose you have a rectangle with a width of $6$ and a height of $3$. You can score $4$ points by coloring all the cells in any $4$ columns, thus performing $12$ operations.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 100$) — the number of test cases. The following are the descriptions of the test cases.

The first line of each test case description contains two integers $n$ and $k$ ($1 \le n \le 1000, 1 \le k \le 100$) — the number of rectangles in the case and the required number of points.

The next $n$ lines contain the descriptions of the rectangles. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 100$) — the width and height of the $i$-th rectangle.

It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $1000$.

Output Format:
For each test case, output a single integer — the minimum number of operations required to score at least $k$ points. If it is impossible to score at least $k$ points, output -1.

Tags:
['dp', 'greedy', 'implementation', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB
You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections.

Every minute, you can board a bus at intersection $u_i$ and travel for $l_{i1}$ minutes to intersection $v_i$. Conversely, you can travel from intersection $v_i$ to intersection $u_i$ in $l_{i1}$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there.

You can also walk along each street, which takes $l_{i2} > l_{i1}$ minutes.

You can make stops at intersections.

You live at intersection number $1$. Today you woke up at time $0$, and you have an important event scheduled at intersection number $n$, which you must reach no later than time $t_0$. You also have a phone call planned that will last from $t_1$ to $t_2$ minutes ($t_1 < t_2 < t_0$).

During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $t_1$ and board the bus again at minute $t_2$.

Since you want to get enough sleep, you became curious — how late can you leave home to have time to talk on the phone and still not be late for the event?

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The following are the descriptions of the test cases.

The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) — the number of intersections and streets in the city.

The second line of each test case contains three integers $t_0$, $t_1$, $t_2$ ($1 \le t_1 < t_2 < t_0 \le 10^9$) — the start time of the event, the start time of the phone call, and its end time, respectively.

The next $m$ lines of each test case contain descriptions of the streets.

The $i$-th line contains four integers $u_i$, $v_i$, $l_{i1}$, $l_{i2}$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le l_{i1} < l_{i2} \le 10^9$) — the numbers of the intersections connected by the $i$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other.

It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $10^5$. It is also guaranteed that the sum of the values of $m$ across all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single integer — the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.

Tags:
['binary search', 'brute force', 'graphs', 'greedy', 'shortest paths']

Time Limit: 4000 ms
Memory Limit: 256 MB
Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $n$ distinct integers $a_1, a_2, \ldots, a_n$.

The set will undergo $m$ operations sequentially. The operations can be of the following types:

  * Insert element $x$ into the set;    * Remove element $x$ from the set;    * Report the $k$-load of the set. 

The $k$-load of the set is defined as the minimum positive integer $d$ such that the integers $d, d + 1, \ldots, d + (k - 1)$ do not appear in this set. For example, the $3$-load of the set $\\{3, 4, 6, 11\\}$ is $7$, since the integers $7, 8, 9$ are absent from the set, and no smaller value fits.

Ksyusha is busy with management tasks, so you will have to write the engine. Implement efficient support for the described operations.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The following lines describe the test cases.

The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the initial size of the set.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 2 \cdot 10^6$) — the initial state of the set.

The third line contains an integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of operations.

The next $m$ lines contain the operations. The operations are given in the following format:

  * + $x$ ($1 \le x \le 2 \cdot 10^6$) — insert element $x$ into the set (it is guaranteed that $x$ is not in the set);    * - $x$ ($1 \le x \le 2 \cdot 10^6$) — remove element $x$ from the set (it is guaranteed that $x$ is in the set);    * ? $k$ ($1 \le k \le 2 \cdot 10^6$) — output the value of the $k$-load of the set. 

It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$, and the same holds for $m$.

Output Format:
For each test case, output the answers to the operations of type "?".

Tags:
['binary search', 'brute force', 'data structures', 'implementation']

Time Limit: 3000 ms
Memory Limit: 512 MB
You are given a cyclic array $a_1, a_2, \ldots, a_n$.

You can perform the following operation on $a$ at most $n - 1$ times:

  * Let $m$ be the current size of $a$, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, $a_m$ and $a_1$ are adjacent and $a_m$ is the previous one), and delete exactly one of them. In other words, choose an integer $i$ ($1 \leq i \leq m$) where $a_i \leq a_{(i \bmod m) + 1}$ holds, and delete exactly one of $a_i$ or $a_{(i \bmod m) + 1}$ from $a$. 

Your goal is to find the minimum number of operations needed to make all elements in $a$ equal.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the length of the array $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of array $a$.

Output Format:
For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in $a$ equal.

Tags:
['greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB
There is an integer sequence $a$ of length $n$, where each element is initially $-1$.

Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to $1$, and another writes letters from right to left with a pointer initially pointing to $n$.

Misuki would choose one of the typewriters and use it to perform the following operations until $a$ becomes a permutation of $[1, 2, \ldots, n]$

  * write number: write the minimum positive integer that isn't present in the array $a$ to the element $a_i$, $i$ is the position where the pointer points at. Such operation can be performed only when $a_i = -1$.    * carriage return: return the pointer to its initial position (i.e. $1$ for the first typewriter, $n$ for the second)    * move pointer: move the pointer to the next position, let $i$ be the position the pointer points at before this operation, if Misuki is using the first typewriter, $i := i + 1$ would happen, and $i := i - 1$ otherwise. Such operation can be performed only if after the operation, $1 \le i \le n$ holds. 

Your task is to construct any permutation $p$ of length $n$, such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the permutation.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a line of $n$ integers, representing the permutation $p$ of length $n$ such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using, or $-1$ if it is impossible to do so.

If there are multiple valid permutations, you can output any of them.

Tags:
['constructive algorithms']

Time Limit: 1500 ms
Memory Limit: 256 MB
This is an interactive problem.

Misuki has chosen a secret tree with $n$ nodes, indexed from $1$ to $n$, and asked you to guess it by using queries of the following type:

  * "? a b" — Misuki will tell you which node $x$ minimizes $|d(a,x) - d(b,x)|$, where $d(x,y)$ is the distance between nodes $x$ and $y$. If more than one such node exists, Misuki will tell you the one which minimizes $d(a,x)$. 

Find out the structure of Misuki's secret tree using at most $15n$ queries!

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 200$) — the number of test cases.

Each test case consists of a single line with an integer $n$ ($2 \le n \le 1000$), the number of nodes in the tree.

It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.

Output Format:


Tags:
['binary search', 'brute force', 'dfs and similar', 'divide and conquer', 'dsu', 'greedy', 'interactive', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given an integer sequence $a_1, a_2, \ldots, a_n$. Let $S$ be the set of all possible non-empty subsequences of $a$ without duplicate elements. Your goal is to find the longest sequence in $S$. If there are multiple of them, find the one that minimizes lexicographical order after multiplying terms at odd positions by $-1$.

For example, given $a = [3, 2, 3, 1]$, $S = \\{[1], [2], [3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 3, 1], [3, 2, 1]\\}$. Then $[2, 3, 1]$ and $[3, 2, 1]$ would be the longest, and $[3, 2, 1]$ would be the answer since $[-3, 2, -1]$ is lexicographically smaller than $[-2, 3, -1]$.

A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements.

A sequence $c$ is lexicographically smaller than a sequence $d$ if and only if one of the following holds:

  * $c$ is a prefix of $d$, but $c \ne d$;   * in the first position where $c$ and $d$ differ, the sequence $c$ has a smaller element than the corresponding element in $d$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). The description of the test cases follows.

The first line of each test case contains an integer $n$ ($1 \le n \le 3 \cdot 10^5$) — the length of $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the sequence $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output the answer in the following format:

Output an integer $m$ in the first line — the length of $b$.

Then output $m$ integers $b_1, b_2, \ldots, b_m$ in the second line — the sequence $b$.

Tags:
['brute force', 'constructive algorithms', 'data structures', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.

Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.

Define the operation $\mathrm{pop}$ as follows:

  1. initialize variable $v$ as $1$;    2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$);      1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them;      2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$);      3. assign $x$ to $v$ (i.e. $v := x$);    3. assign $-1$ to $a_v$ (i.e. $a_v := -1$). 

Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.

A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.

A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first time.

Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:

  * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$. 

Two heaps are considered different if there is a node which has different values in the heaps.

Since the answer might be large, print it modulo $p$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

The first line of each test case contains three integers $n, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).

It is guaranteed that the sum of $n$ and the sum of $k$ over all test cases does not exceed $500$.

Output Format:
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.

Tags:
['combinatorics', 'dp', 'math', 'trees']

Time Limit: 3000 ms
Memory Limit: 512 MB