For a string $$$s$$$, let $$$f(s)$$$ be the string obtained by merging possibly several consecutive equal characters into a single character. For example, $$$f(abbbccdc) = abcdc$$$.
Given a string $$$a$$$, find the shortest substring $$$b$$$ of $$$a$$$ such that $$$f(a)$$$ is equal to $$$f(b)$$$.
The first line will have $$$t$$$, the number of test cases – ($$$1 \le t \le 50$$$).
The first line of each test case will have $$$n$$$, the length of string $$$a$$$ – ($$$1 \le n \le 100$$$).
The second line of each test case will have string $$$a$$$ of length $$$n$$$ which contains only lower case characters from $$$a$$$ to $$$z$$$.
For each test case, output a single line with string $$$b$$$.
28abbbccdc3zzz
abbbccdc z
Given integers $$$n$$$ and $$$k$$$, construct a permutation $$$p$$$ of integers $$$1 ... n$$$ that maximizes the value of $$$k$$$ after the following process.
Perform $$$n$$$ operations, on the $$$i$$$-th operation ($$$i = 1, 2, ..., n$$$)
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) – the number of test cases.
The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^5$$$).
The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
For each test case, output the permutation $$$p_1, p_2, ..., p_n$$$ on a single line.
If there are multiple such permutations, output any.
21 32 4
1 2 1
In the first test case, $$$n = 1$$$, so there is only one permutation.
In the second test case, it is optimal to multiply $$$k$$$ by $$$2$$$ first and then divide it by $$$1$$$.
Given $$$3$$$ integers $$$x$$$, $$$y$$$, and $$$z$$$, you can perform three types of operations on them.
Swap the values of any $$$2$$$ of them ($$$x$$$ and $$$y$$$ or $$$x$$$ and $$$z$$$ or $$$y$$$ and $$$z$$$).
Choose one of them and increase the value of it by $$$1$$$
Choose one of them that is greater than $$$1$$$ and decrease the value of it by $$$1$$$.
Whats the minimum number of operations to make $$$x$$$ a factor of both $$$y$$$ and $$$z$$$?
On the first line there will be the number of tests $$$t$$$ – ($$$1 \le t \le 100$$$).
On the first line of each test case there will be the $$$x$$$, $$$y$$$, and $$$z$$$ – ($$$1 \le x, y, z \le 10^4$$$).
The answer for each test case will be a single integer on a single line.
410 25 3510 9 111 10 203 2 1
4 2 0 1
In the first test case, we can decrease $$$x$$$ by $$$1$$$, increase $$$y$$$ by $$$2$$$, and increase $$$z$$$ by $$$1$$$.
You are given a strictly increasing array $$$a$$$ of length $$$n$$$ ($$$a_1 \lt a_2 \lt \ldots \lt a_n$$$).
Determine whether it is possible to insert $$$k$$$ integers $$$b_1 \le b_2 \le \ldots \le b_k$$$ arbitrarily into $$$a$$$, such that $$$b_1 \ge a_1$$$, $$$b_k \le a_n$$$, and $$$a$$$ is strictly increasing after all $$$k$$$ integers have been inserted.
Since this problem is too easy, solve the problem for $$$q$$$ point updates on $$$a$$$.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \le n \le 2\cdot 10^5, 0 \le k \le 10^9$$$, $$$1 \le q \le 2\cdot 10^5$$$) — the length of $$$a$$$, the number of integers to insert into $$$a$$$, and the number of point updates, respectively.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the strictly increasing array $$$a$$$ itself.
Each of the next $$$q$$$ lines of each test case contains two integers $$$i$$$ and $$$x$$$ ($$$1 \le i \le n, 1 \le x \le 10^9$$$) — representing a point update to set $$$a_i:=x$$$.
It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases both do not exceed $$$2\cdot 10^5$$$.
Output $$$q$$$ lines, with the $$$q_i$$$-th line being "YES" if it is possible to insert $$$k$$$ integers into $$$a$$$ after the $$$i$$$-th point update, or being "NO" if it is impossible. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
24 2 41 2 4 54 62 34 91 310 1 41 2 3 4 5 6 7 8 9 101 19 1110 1110 12
YES YES YES NO NO NO NO YES
You are given an array $$$a$$$ of length $$$n$$$, and an initially empty array $$$b$$$.
You may perform the following operation at most once:
For instance, choosing the indices $$$l = 2$$$ and $$$r = 5$$$ for the array $$$[1, 5, 2, 3, 4, 6]$$$ will result in the new array $$$[1, 2, 3, 4, 5, 6]$$$ after the operation is performed.
After performing the operation (if you choose to do so), you will build another array $$$b$$$ with the following process: on the $$$i$$$th turn of the process, you can insert $$$a_i$$$ at either the front or back of $$$b$$$.
Determine if there exists a way to construct $$$b$$$ such that $$$b$$$ is sorted in non-decreasing order after all $$$n$$$ elements are inserted.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 4000$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 4000$$$) — the length of array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4000$$$.
For each test case, print "YES" if there exists a way to construct $$$b$$$ such that it is sorted in non-decreasing order, or print "NO" if it is impossible. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
361 5 2 3 4 661 3 2 4 5 476 12 4 9 4 9 15
YES NO YES
For the first test case, choose indices $$$l = 2$$$ and $$$r = 5$$$. Then the array becomes sorted, and inserting each element to the back of $$$b$$$ will result in $$$b$$$ being sorted.
George's favorite integer is $$$7$$$. Unfortunately, $$$7$$$ is only one integer among infinitely many. Thus, George also likes integers with first digit $$$7$$$, last digit $$$7$$$, or both.
George has an array $$$a_{1...n}$$$ of $$$n$$$ integers. Count the subarrays of $$$a$$$ such that George likes the sum of the elements of the subarray. Formally, count the number of pairs $$$(i, j)$$$, where $$$1 \le i \le j \le n$$$, such that George likes $$$\displaystyle\sum_{k=i}^{j} a_k$$$.
The first line contains $$$n$$$ ($$$1 \le n \le 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$0 \le a_i \le 10^9$$$).
Output one integer: the number of subarrays of $$$a$$$ such that George likes the sum of the elements of the subarray.
522 4 3 46 2
3
George likes the sums of $$$[22, 4, 3, 46]$$$, $$$[4, 3]$$$, and $$$[22, 4, 3, 46, 2]$$$.
Miyamura has baked a yummy cake for Hori-san. He wants to paint the top of the cake, which can be represented by an $$$n$$$ by $$$m$$$ grid. Unfortunately, his only paintbrush is a cube that is colored blue on two opposite sides and red on the remaining sides. Miyamura knows that Hori-san hates blueberries, so he wants to maximize the number of red cells.
Denote the upper left corner of the cake as $$$(1, 1)$$$ and the bottom right as $$$(n, m)$$$. Miyamura can choose the starting position and orientation.
In order to color the cake, Miyamura can roll the brush onto the adjacent cells such that one edge of the cubical brush touches the cake during the duration of the roll, coloring that cell of the cake with the color of the side of the brush that lands on it.
Miyamura cannot rotate the brush and stay in the same cell, and he cannot roll the paintbrush off the canvas. If he rolls on a cell that is already covered, he will replace the color of the cell with the new color.
Your task is to help him find a sequence of moves such that all cells are colored and the number of red cells is maximized.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). The description of the test cases follows.
The first line and only line each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$), denoting the dimensions of the grid.
The first line of each test case will have the maximum number of cells that can be painted red.
The second line will have two integers $$$a$$$, $$$b$$$ and a single character $$$c$$$ denoting the starting configuration, where:
The third line will have an integer $$$q$$$ ($$$0 \le q \le 25000$$$) denoting the number of rolls your solution performs. It is guaranteed that a sequence of operations of at most $$$25000$$$ exists to color the cake optimally.
The fourth line will contain a string of length $$$q$$$. Each character of the string is either $$$L$$$, $$$R$$$, $$$D$$$, or $$$U$$$, indicating that at that step, the cube rolled left, right, down, or up respectively.
Note that you do not need to minimize $$$q$$$. If there are multiple valid solutions, print any of them.
31 12 21 2
1 1 1 | 0 3 1 1 | 5 RDLRU 2 1 1 | 1 R
We use 'B' to denote the blue cells, 'R' to denote the red cells and '.' to denote the uncolored cells.
For the first testcase, the number of red cells is $$$1$$$, and the operation process can be represented as follows.
|
For the second testcase, the number of red cells is $$$3$$$, and the operation process can be represented as follows.
|
|
|
|
|
|
For the third testcase, the number of red cells is $$$2$$$, and the operation process can be represented as follows.
|
|
For an array $$$x$$$, let $$$f(x)$$$ be the array obtained by deleting all $$$x_i$$$ such that there exists some $$$j \lt i$$$, and $$$x_j$$$ is equal to $$$x_i$$$, and concatenating the rest together. In other words, it keeps the first occurrence of each value and concatenates them in the order they appear in the $$$x$$$. For example, $$$f([1, 2, 1, 3]) = [1, 2, 3]$$$.
Given an array $$$a$$$, find the length of the shortest subarray $$$b$$$ of $$$a$$$ such that $$$f(a)$$$ is equal to $$$f(b)$$$.
A subarray is an array obtained by deleting several (possibly zero) elements from the beginning and end of the original array.
The first line has $$$t$$$, the number of testcases. — ($$$1 \le t \le 3 \cdot 10^5$$$)
The first line of each test case has $$$n$$$, the length of $$$a$$$. — ($$$1 \le n \le 3 \cdot 10^5$$$)
The second line of each test case has $$$n$$$ integers, which is the array $$$a$$$. — ($$$1 \le a_i \le n$$$)
The sum of $$$n$$$ over all test cases will not exceed $$$3 \cdot 10^5$$$.
The output of each test case has a single integer, the length of the shortest subarray.
391 1 2 2 3 3 1 2 3121 2 2 2 2 2 2 3 1 2 1 3131 2 2 2 2 2 3 4 1 4 2 3 4
3 4 8
In the first test case, the optimal subarray is the last 3 elements.
An array $$$c$$$ of length $$$n$$$ is good if it satisfies the following property:
We can measure the cost of some array $$$b$$$ of $$$n$$$ integers. In one operation, you can remove any good subsequence from the array. The cost of $$$b$$$ is the minimum number of operations to make the array empty.
You're given an array $$$a$$$ of length $$$n$$$. You want to minimise the cost of $$$a$$$ by performing the following action exactly once:
Please output the minimum cost after performing the following action exactly once on $$$a$$$.
Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the size 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 3 \cdot 10^5$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, output a single integer — the minimum cost of $$$a$$$ after performing the given action exactly once.
41010 9 8 7 6 5 4 3 2 1828 14 7 27 9 3 5 1545 10 3 643 5 6 6
3 2 1 2
In the first sample, one optimal selection is to perform the action with $$$l = 2$$$ and $$$r = 5$$$. After performing the action, $$$a = [10, 6, 9, 8, 7, 5, 4, 3, 2, 1]$$$. We can show that the cost of this $$$a$$$ is equal to $$$3$$$:
Given an array $$$a$$$ with $$$n$$$ elements, $$$n = 4^p, 0 \le p \le 10, -10^9 \le a_i \le 10^9$$$, you can perform the following operation.
1. Choose an integer $$$k, 0 \le k \le p$$$.
2. Divide $$$a$$$ into $$$4^k$$$ consecutive segments of length $$$4^{p - k}$$$.
3. Choose one of these segments and either add $$$1$$$ or subtract $$$1$$$ from all the elements in that segment.
Find the minimum number of operations to make the entire array $$$0$$$.
The first line has a single integer $$$t$$$, the number of tests. ($$$1 \le t \le 4^{9}$$$)
The first line of each test case has a single integer $$$n$$$. ($$$1 \le n \le 4^{10}$$$)
The second line of each test case has $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$. ($$$-10^9 \le a_i \le 10^9$$$)
The sum of $$$n$$$ over all test cases doesn't exceed $$$4^{10}$$$.
For each test case output a single integer, the minimum number of operations.
31341 4 3 4161 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1
3 7 3
Two best friends — Alice and Bob — are playing a Minecraft modpack when they reach the final boss. The final boss is based on the Hydra, a mythical beast which is said to grow back two heads after you chop off one of them. Luckily, the mod developers made the boss possible to beat, so it works slightly differently than that.
The hydra is initially made of $$$n$$$ heads, the $$$i$$$-th head having a strength level of $$$a_i$$$, and there is an additional parameter $$$k$$$. If Alice or Bob attacks one of the heads having $$$a_i \gt k$$$ with integer power $$$p$$$ ($$$1 \le p \le a_i$$$) — which you can choose per-attack — the strength level of the head decreases by $$$p$$$, and another head grows with $$$m$$$ strength such that $$$m$$$ is the smallest integer with $$$m^2 \ge p$$$. If any head with $$$a_i \le k$$$ is attacked, it is instantly severed and no heads regrow. Otherwise, if $$$a_i$$$ falls to zero after the attack, the head is severed, and the Hydra perishes if all heads are severed and no new ones are about to grow.
Whoever gets the final hit on the Hydra gets an achievement. Alice asks you to decide whether she should hit first or second if they both play optimally to try to get the achievement.
The first line of input contains $$$t$$$, the number of testcases ($$$1 \le t \le 10^4$$$).
The first line of each testcase contains $$$n$$$ and $$$k$$$, the number of heads that the Hydra begins with and the value of the parameter respectively ($$$1 \le n \le 10^6$$$, $$$2 \le k \le 10^9$$$).
The second line of each testcase contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), the strength level of the heads which the Hydra begins with.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
Output the word 'First' if Alice should make the first strike in order to get the achievement, or 'Second' if she should let Bob go for it.
51 1064 525 5 6 34 385 30 3 73 44 5 64 21 2 5 9
FirstSecondSecondFirstSecond
Given an array of positive integers, define one quicksort operation as the following:
For example, if we have the array $$$\{5, 4, 6, 5, 3, 1, 4, 2\}$$$ and we choose the pivot to be $$$4$$$, the new array becomes $$$\{3, 1, 2, 4, 4, 5, 6, 5\}$$$.
Given an array of $$$n$$$ integers and $$$q$$$ modification queries in the form $$$a_i = v$$$, find the minimum number of quicksort operations required to sort the array into ascending order after each operation. The updates are permanent.
The first line contains $$$n$$$ ($$$1 \le n \le 10^5$$$).
The second line contains $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
The third line contains $$$q$$$ ($$$1 \le q \le 10^5$$$).
Each of the next $$$q$$$ lines contains $$$i$$$ and $$$v$$$ ($$$1 \le i \le n$$$, $$$1 \le v \le 10^9$$$), indicating a modification query in the form $$$a_i = v$$$.
After each modification query, print the minimum number of quicksort operations required to sort the array.
88 5 4 6 1 7 3 587 87 46 64 14 72 85 45 7
3 3 2 2 3 3 2 2