inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Fox loves permutations! She came up with the following problem and asked Cat to solve it:

You are given an even positive integer $n$ and a permutation$^\dagger$ $p$ of length $n$.

The score of another permutation $q$ of length $n$ is the number of local maximums in the array $a$ of length $n$, where $a_i = p_i + q_i$ for all $i$ ($1 \le i \le n$). In other words, the score of $q$ is the number of $i$ such that $1 < i < n$ (note the strict inequalities), $a_{i-1} < a_i$, and $a_i > a_{i+1}$ (once again, note the strict inequalities).

Find the permutation $q$ that achieves the maximum score for given $n$ and $p$. If there exist multiple such permutations, you can pick any of them.

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input Format:
The first line of input contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases in the input you will have to solve.

The first line of each test case contains one even integer $n$ ($4 \leq n \leq 10^5$, $n$ is even) — the length of the permutation $p$.

The second line of each test case contains the $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$). It is guaranteed that $p$ is a permutation of length $n$.

It is guaranteed that the sum of $n$ across all test cases doesn't exceed $10^5$.

Output Format:
For each test case, output one line containing any permutation of length $n$ (the array $q$), such that $q$ maximizes the score under the given constraints.

Tags:
['constructive algorithms', 'greedy', 'implementation', 'math', 'sortings']

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

Fox gave Cat two positive integers $n$ and $k$. She has a hidden array $a_1, \ldots , a_n$ of length $n$, such that $1 \leq a_i \leq n$ for every $i$. Now they are going to play the following game:

For any two integers $l, r$ such that $1 \leq l \leq r \leq n$, define $f(l, r) = (r - l + 1) \cdot \max\limits_{x=l}^r a_x$. In other words, $f(l, r)$ is equal to the maximum of the subarray $a_l, \ldots, a_r$ multiplied by its size.

Cat can ask Fox at most $2 n$ questions about the array. He will tell her two integers $l$ and $x$ ($1 \leq l \leq n, 1 \leq x \leq 10^9$), and she will tell him one integer $p$ as the answer — the smallest positive integer $r$ such that $f(l, r) = x$, or $n+1$ if no such $r$ exists.

Now, Cat needs to find the largest value $m$ such that there exists a sequence $c_1, \ldots, c_{k-1}$ such that $1 \leq c_1 < \ldots < c_{k-1} < n$ and $f(1, c_1) = f(c_1 + 1, c_2) = \ldots = f(c_{k-1}+1, n) = m$. If no such $m$ exists, he should indicate this and take $-1$ as the answer. Note that for $k = 1$, $m$ is always equal to $f(1, n)$.

In other words, the goal is to find the largest $m$ such that you can split the array into exactly $k$ subarrays ($k$ is the constant given to you in the beginning of the interaction) so that all the subarrays have the product of their length and their maximum equal to $m$, or determine that no such $m$ exists. Every element should belong in exactly one of the subarrays.

Cat doesn't know what he should do, so he asked you to play the game for him.

Input Format:


Output Format:


Tags:
['brute force', 'interactive', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB
Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

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

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

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

Output Format:
For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

Tags:
['graphs', 'math', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ — the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x_k$ are divisible by $z$.

You are given three arrays $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$ and $c_1, c_2, \ldots, c_n$ of length $n$, containing positive integers.

You also have a machine that allows you to swap $a_i$ and $b_i$ for any $i$ ($1 \le i \le n$). Each swap costs you $c_i$ coins.

Find the maximum possible value of $$\gcd(a_1, a_2, \ldots, a_n) + \gcd(b_1, b_2, \ldots, b_n)$$ that you can get by paying in total at most $d$ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the $q$ possible values $d_1, d_2, \ldots, d_q$.

Input Format:
There are two integers on the first line — the numbers $n$ and $q$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq q \leq 5 \cdot 10^5$).

On the second line, there are $n$ integers — the numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^8$).

On the third line, there are $n$ integers — the numbers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 10^8$).

On the fourth line, there are $n$ integers — the numbers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$).

On the fifth line, there are $q$ integers — the numbers $d_1, d_2, \ldots, d_q$ ($0 \leq d_i \leq 10^{15}$).

Output Format:
Print $q$ integers — the maximum value you can get for each of the $q$ possible values $d$.

Tags:
['bitmasks', 'brute force', 'dp', 'implementation', 'number theory']

Time Limit: 5000 ms
Memory Limit: 512 MB
Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \times 3$, i.e., five rows and three columns.

There are $x$ applications with an icon size of $1 \times 1$ cells; such an icon occupies only one cell of the screen. There are also $y$ applications with an icon size of $2 \times 2$ cells; such an icon occupies a square of $4$ cells on the screen. Each cell of each screen can be occupied by no more than one icon.

Rosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed.

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

The first and only line of each test case contains two integers $x$ and $y$ ($0 \leq x, y \leq 99$) — the number of applications with a $1 \times 1$ icon and the number of applications with a $2 \times 2$ icon, respectively.

Output Format:
For each test case, output the minimal number of required screens on a separate line.

Tags:
['greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Polycarp has a string $s$, which consists of lowercase Latin letters. He encodes this string using the following algorithm:

  * first, he constructs a new auxiliary string $r$, which consists of all distinct letters of the string $s$, written in alphabetical order;    * then the encoding happens as follows: each character in the string $s$ is replaced by its symmetric character from the string $r$ (the first character of the string $r$ will be replaced by the last, the second by the second from the end, and so on). 

For example, encoding the string $s$="codeforces" happens as follows:

  * the string $r$ is obtained as "cdefors";    * the first character $s_1$='c' is replaced by 's';    * the second character $s_2$='o' is replaced by 'e';    * the third character $s_3$='d' is replaced by 'r';    * ...    * the last character $s_{10}$='s' is replaced by 'c'. 

![](CDN_BASE_URL/e120531cc984becdbbab53f80507a062) The string $r$ and replacements for $s$="codeforces".

Thus, the result of encoding the string $s$="codeforces" is the string "serofedsoc".

Write a program that performs decoding — that is, restores the original string $s$ from the encoding result.

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

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

The second line of each test case contains a string $b$ of length $n$, consisting of lowercase Latin letters — the result of encoding the original string $s$.

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

Output Format:
For each test case, output the string $s$ from which the encoding result $b$ was obtained.

Tags:
['implementation', 'sortings', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.

Polycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:

  * $b_1 \ne c_1$ and $b_2 = c_2$ and $b_3 = c_3$;    * $b_1 = c_1$ and $b_2 \ne c_2$ and $b_3 = c_3$;    * $b_1 = c_1$ and $b_2 = c_2$ and $b_3 \ne c_3$. 

Find the number of beautiful pairs of triples among the written triples $[a_j, a_{j + 1}, a_{j + 2}]$.

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

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

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

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

Output Format:
For each test case, output a single integer — the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$.

Note that the answer may not fit into 32-bit data types.

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

Time Limit: 4000 ms
Memory Limit: 256 MB
Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates $(0, 0)$. A set of instructions $s$ consisting of $n$ instructions of the following types was specially developed for them:

  * N: move one meter north (from point $(x, y)$ to $(x, y + 1)$);    * S: move one meter south (from point $(x, y)$ to $(x, y - 1)$);    * E: move one meter east (from point $(x, y)$ to $(x + 1, y)$);    * W: move one meter west (from point $(x, y)$ to $(x - 1, y)$). 

Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all $n$ instructions, the helicopter and the rover end up at the same point, or determine that this is impossible.

Input Format:
The first line of input contains $t$ ($1 \leq t \leq 10^4$) — the number of 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 instructions.

The second line of each test case contains a string $s$ of length $n$ consisting of the characters 'N', 'S', 'E', 'W' — the sequence of instructions.

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, if the required distribution of instructions exists, output a string $p$ of length $n$ consisting of the characters 'R', 'H'. If the $i$-th operation should be executed by the rover, then $p_i=\text{R}$, if the $i$-th operation should be executed by the helicopter, then $p_i=\text{H}$. If there are multiple solutions, output any of them.

Otherwise, output NO.

Tags:
['constructive algorithms', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
Being a physicist, Charlie likes to plan his life in simple and precise terms.

For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain happiness $h_i$.

Borrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$).

Since physicists don't code, help Charlie find the maximum obtainable sum of happiness.

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

The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 50$, $1 \le x \le 10^8$) — the total number of months and the monthly salary.

The $i$-th of the following $m$ lines contains two integers, $c_i$ and $h_i$ ($0 \le c_i \le 10^8$, $1 \le h_i \le 10^3$) — the cost and happiness on offer for the $i$-th month. Note that some happiness may be free ($c_i=0$ for some $i$'s).

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

Output Format:
For each test case, print a single integer, the maximum sum of happiness Charlie could obtain.

Tags:
['dp']

Time Limit: 3000 ms
Memory Limit: 256 MB
Alice and Bob were playing a game again. They have a grid of size $a \times b$ ($1 \le a, b \le 10^9$), on which there are $n$ chips, with at most one chip in each cell. The cell at the intersection of the $x$-th row and the $y$-th column has coordinates $(x, y)$.

Alice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character 'U', 'D', 'L', or 'R' and an integer $k$:

  * If the character is 'U', then the first $k$ remaining rows will be cut;    * If the character is 'D', then the last $k$ remaining rows will be cut;    * If the character is 'L', then the first $k$ remaining columns will be cut;    * If the character is 'R', then the last $k$ remaining columns will be cut. 

Based on the initial state of the grid and the players' moves, determine the number of points earned by Alice and Bob, respectively.

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

The first line of each test case contains four integers $a$, $b$, $n$, and $m$ ($2 \le a, b \le 10^9$, $1 \le n, m \le 2 \cdot 10^5$) — the dimensions of the grid, the number of chips, and the number of moves.

Each of the next $n$ lines contain two integers $x_i$ and $y_i$ ($1 \le x_i \le a$, $1 \le y_i \le b$) — the coordinates of the chips. All pairs of coordinates are distinct.

Each of the next $m$ lines contain a character $c_j$ and an integer $k_j$ — the description of the $j$-th move. It is guaranteed that $k$ is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move.

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

Output Format:
For each test case, output two integers — the number of points earned by Alice and Bob, respectively.

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

Time Limit: 3000 ms
Memory Limit: 256 MB
You can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.

Being a physicist, Charlie likes to plan his life in simple and precise terms.

For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain one unit of happiness. You cannot buy more than one unit each month.

Borrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$).

Since physicists don't code, help Charlie find the maximum reachable units of happiness.

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

The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 2 \cdot 10^5$, $1 \le x \le 10^3$) — the total number of months and the monthly salary.

The second line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \leq c_i \leq 10^3$) — the cost of one unit of happiness for each month.

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

Output Format:
For each test case, output one integer — the maximal amount of happiness Charlie can get.

Tags:
['data structures', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Mocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift.

Now Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the following operation some (possibly, zero) times:

  * Split the array into two parts — a prefix and a suffix, then swap these two parts. In other words, let $a=x+y$. Then, we can set $a:= y+x$. Here $+$ denotes the array concatenation operation. 

For example, if $a=[3,1,4,1,5]$, we can choose $x=[3,1]$ and $y=[4,1,5]$, satisfying $a=x+y$. Then, we can set $a:= y + x = [4,1,5,3,1]$. We can also choose $x=[3,1,4,1,5]$ and $y=[\,]$, satisfying $a=x+y$. Then, we can set $a := y+x = [3,1,4,1,5]$. Note that we are not allowed to choose $x=[3,1,1]$ and $y=[4,5]$, neither are we allowed to choose $x=[1,3]$ and $y=[5,1,4]$, as both these choices do not satisfy $a=x+y$.

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

The first line of each test case contains a single integer $n$ ($2\leq n\leq 50$) — the length of the array $a$.

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

Output Format:
For each test case, output "Yes" if $a$ could become non-decreasing after performing the operation any number of times, and output "No" if not.

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

Tags:
['brute force', 'greedy', 'implementation', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Mocha likes arrays, so before her departure, 378QAQ gave her an array $a$ consisting of $n$ positive integers as a gift.

Mocha thinks that $a$ is beautiful if there exist two numbers $i$ and $j$ ($1\leq i,j\leq n$, $i\neq j$) such that for all $k$ ($1 \leq k \leq n$), $a_k$ is divisible$^\dagger$ by either $a_i$ or $a_j$.

Determine whether $a$ is beautiful.

$^\dagger$ $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y \cdot z$.

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

The first line of each test case contains a single integer $n$ ($3\leq n\leq 10^5$) — the length of the array $a$.

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

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

Output Format:
For each test case, output "Yes" if array $a$ is beautiful, and output "No" otherwise.

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

Tags:
['brute force', 'greedy', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Mocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift.

Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:

  1. Choose indices $l$ and $r$ ($1 \leq l < r \leq n$)    2. Let $x$ be the median$^\dagger$ of the subarray $[a_l, a_{l+1},\ldots, a_r]$    3. Set all values $a_l, a_{l+1},\ldots, a_r$ to $x$ 

Suppose $a=[1,2,3,4,5]$ initially:

  * If Mocha chooses $(l,r)=(3,4)$ in the first operation, then $x=3$, the array will be changed into $a=[1,2,3,3,5]$.    * If Mocha chooses $(l,r)=(1,3)$ in the first operation, then $x=2$, the array will be changed into $a=[2,2,2,4,5]$. 

Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.

$^\dagger$ The median in an array $b$ of length $m$ is an element that occupies position number $\lfloor \frac{m+1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example, the median of $[3,1,4,1,5]$ is $3$ and the median of $[5,25,20,24]$ is $20$.

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

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

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

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

Output Format:
For each test case, output the maximum value of the number.

Tags:
['binary search', 'brute force', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB