Alice and Bob are given a binary string$$$^{\text{∗}}$$$ $$$s$$$ of length $$$n$$$. It contains at least one $$$\mathtt{0}$$$ and at least one $$$\mathtt{1}$$$.
They each perform exactly one operation in the following order:
Alice wants the final string to be lexicographically$$$^{\text{†}}$$$ as large as possible, while Bob wants it to be lexicographically as small as possible. Determine the final string if both players act optimally.
$$$^{\text{∗}}$$$A binary string is a string consisting only of the characters $$$\mathtt{0}$$$ and $$$\mathtt{1}$$$.
$$$^{\text{†}}$$$For two distinct binary strings $$$a$$$ and $$$b$$$ of the same length, $$$a$$$ is lexicographically smaller than $$$b$$$ if, at the first position where they differ, $$$a$$$ has the smaller digit.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The only line of each test case contains a binary string $$$s$$$ of length $$$n$$$ ($$$3 \le n \le 100$$$).
It is guaranteed that $$$s$$$ contains at least one $$$\mathtt{0}$$$ and at least one $$$\mathtt{1}$$$.
For each test case, output the final string if both players act optimally.
41011100100100101010000010100100101
11010001010000010100100101
In the first test case, Alice must delete the only $$$\mathtt{0}$$$. Bob may delete either occurrence of $$$\mathtt{1}$$$, so the resulting string is $$$\mathtt{1}$$$.
In the second test case, Alice may delete either occurrence of $$$\mathtt{0}$$$. Bob optimally deletes one of the first two occurrences of $$$\mathtt{1}$$$, so the resulting string is $$$\mathtt{101}$$$.
In the third test case, Alice may delete any occurrence of $$$\mathtt{0}$$$. Bob then deletes the only occurrence of $$$\mathtt{1}$$$, so the resulting string is $$$\mathtt{00}$$$.
You are given two arrays $$$a$$$ and $$$b$$$ of lengths $$$n$$$ and $$$m$$$, respectively. All $$$n + m$$$ integers in these arrays are distinct.
You can perform the following operation on $$$a$$$ any number of times (possibly, zero):
After performing all operations, you may arrange the elements of $$$a$$$ in any order.
Determine whether it is possible to make $$$a$$$ equal to $$$b$$$.
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.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the lengths of the arrays $$$a$$$ and $$$b$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le 10^9$$$).
It is guaranteed that all $$$n + m$$$ integers in the arrays $$$a$$$ and $$$b$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print "YES" if it is possible to make $$$a$$$ equal to $$$b$$$, and "NO" otherwise.
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.
112 11 323 21 3 52 44 21 3 5 74 64 22 5 8 111 104 21 4 7 93 105 210 1 100 6 490 56 31 4 10 20 30 403 15 354 21 2 3 1004 57 31 8 3 30 18 12 252 15 284 11 2 3 544 21 3 5 62 4
YESNOYESNONOYESYESNOYESYESYES
In the first test case, choose $$$1$$$ and $$$3$$$, delete them, and insert $$$2$$$.
In the second test case, it can be shown that no sequence of operations can make $$$a$$$ equal to $$$b$$$.
In the third test case, choose $$$1$$$ and $$$5$$$, delete them, and insert $$$4$$$. Then, choose $$$3$$$ and $$$7$$$, delete them, and insert $$$6$$$.
In the sixth test case, first choose $$$1$$$ and $$$10$$$, delete them, and insert $$$5$$$. Next, choose $$$6$$$ and $$$100$$$, delete them, and insert $$$90$$$. Finally, choose $$$4$$$ and $$$5$$$, delete them, and insert $$$5$$$.
In the seventh test case, for the pairs $$$(1,4)$$$, $$$(10,20)$$$, and $$$(30,40)$$$, delete both elements and insert $$$3$$$, $$$15$$$, and $$$35$$$, respectively.
In the ninth test case, delete the pairs $$$(1,3)$$$, $$$(8,18)$$$, and $$$(25,30)$$$ and insert $$$2$$$, $$$15$$$, and $$$28$$$, respectively. Then, delete $$$12$$$ and $$$15$$$ and insert $$$15$$$.
In the tenth test case, first choose $$$1$$$ and $$$2$$$, delete them, and insert $$$2$$$. Then, choose $$$2$$$ and $$$3$$$, delete them, and insert $$$3$$$. Finally, choose $$$3$$$ and $$$5$$$, delete them, and insert $$$4$$$.
You are given an array $$$a$$$ of length $$$2n$$$. Each integer from $$$1$$$ to $$$n$$$ occurs exactly twice in $$$a$$$.
Initially, your score is $$$0$$$.
You can repeatedly perform the following operation while $$$a$$$ is non-empty:
Find the maximum possible score after making the array empty.
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.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).
The second line contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \le a_i \le n$$$).
It is guaranteed that each integer from $$$1$$$ to $$$n$$$ occurs exactly twice in $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer — the maximum possible score.
611 121 2 1 221 2 2 131 1 2 3 3 231 2 3 3 2 141 2 3 4 1 2 3 4
41016203628
In the second test case, one optimal strategy is to choose $$$x = 1$$$ first. This deletes the subarray $$$[1, 2, 1]$$$ and adds $$$3^2 = 9$$$ to the score. The remaining array is $$$[2]$$$; choosing $$$x = 2$$$ adds $$$1$$$. The total score is $$$10$$$.
In the third test case, choosing $$$x = 1$$$ deletes the whole array and adds $$$4^2 = 16$$$ to the score.
In the fourth test case, choose $$$x = 1$$$ first and then choose $$$x = 2$$$. The total score is $$$2^2 + 4^2 = 20$$$.
In the sixth test case, choose $$$x = 2$$$ first. This deletes the subarray $$$[2, 3, 4, 1, 2]$$$ from the middle of the array and adds $$$5^2 = 25$$$ to the score. After deleting this subarray and concatenating the remaining elements, the array becomes $$$[1, 3, 4]$$$. Choosing each of the three remaining values then adds $$$1$$$, so the total score is $$$28$$$.
You are given two binary strings $$$s$$$ and $$$t$$$, both of length $$$n$$$.
For two binary strings $$$a$$$ and $$$b$$$ of the same length, the pair $$$(a, b)$$$ is called good if both strings can be made empty by performing the following operation zero or more times:
A binary string may have both $$$\mathtt{0}$$$ and $$$\mathtt{1}$$$ as modes.
You need to answer $$$q$$$ queries. In each query, you are given two integers $$$l$$$ and $$$r$$$. Determine whether the pair of substrings $$$(s_l s_{l+1} \ldots s_r, t_l t_{l+1} \ldots t_r)$$$ is good.
The queries are independent.
$$$^{\text{∗}}$$$For a binary string $$$z$$$, a character $$$c$$$ is a mode if it appears at least $$$\left\lceil \frac{|z|}{2} \right\rceil$$$ times in $$$z$$$. Here, $$$\lceil x \rceil$$$ denotes the smallest integer greater than or equal to $$$x$$$.
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.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the length of each string and the number of queries.
The second line of each test case contains the binary string $$$s$$$ of length $$$n$$$.
The third line of each test case contains the binary string $$$t$$$ of length $$$n$$$.
Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — a query.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each query, output "YES" if the pair $$$(s_l s_{l+1} \ldots s_r, t_l t_{l+1} \ldots t_r)$$$ is good. Otherwise, output "NO".
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.
32 201101 11 25 111111111111 35 100011011111 5
NOYESYESYES
In the first test case, for the first query, the pair is $$$(\mathtt{0}, \mathtt{1})$$$. No character is a mode of both strings, so no operation can be performed.
For the second query, choose both positions and $$$c = \mathtt{0}$$$. Then $$$x = \mathtt{01}$$$ and $$$y = \mathtt{10}$$$, so $$$c$$$ is a mode of both strings and the whole pair is deleted.
In the third test case, neither character is a mode of both $$$\mathtt{00011}$$$ and $$$\mathtt{01111}$$$, so the whole pair cannot be deleted in one operation. It can be emptied in three operations:
You are given three integers $$$n$$$, $$$m$$$, and $$$d$$$, and two arrays $$$p_1, p_2, \ldots, p_m$$$ and $$$r_1, r_2, \ldots, r_m$$$. The array $$$p$$$ is strictly increasing.
For a binary array $$$a$$$ of any positive finite length (so $$$|a|$$$ need not equal $$$n$$$), define its value $$$f(a)$$$ using the following pseudocode:
function f(a):
v := 0
c := 0
for i from 1 to length(a):
if a[i] is equal to 1:
v := v + d
c := c + 1
else:
c := 0
for j from 1 to m:
if c is equal to p[j]:
v := v + r[j]
if c is equal to n:
c := 0
return v
Here, ":=" denotes the assignment operation.Let $$$I(a)$$$ denote the array $$$[1,1,\ldots,1]$$$ of length $$$|a|$$$. In other words, $$$I(a)$$$ consists of $$$|a|$$$ ones.
Determine whether there exists a binary (consisting only of zeros and ones) array $$$a$$$ such that $$$f(a) \gt f(I(a))$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2000$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$d$$$ ($$$1 \le n \le 10^9$$$, $$$0 \le m \le 2000$$$, $$$0 \le d \le 10^9$$$).
The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$p_i$$$ and $$$r_i$$$ ($$$1 \le p_i \le n$$$, $$$1 \le r_i \le 10^9$$$).
The array $$$p$$$ is strictly increasing.
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$.
For each test case, output "YES" if such a binary array $$$a$$$ exists, and "NO" otherwise.
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.
36 4 32 53 94 15 37 3 52 54 57 10684492057 3 386217943367971233 991739271612599954 429216213684492056 402931836
YESNOYES
In the first test case, you can choose $$$a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1]$$$.
The array $$$a$$$ contains $$$16$$$ ones, so the total contribution of $$$d$$$ is $$$16 \cdot 3 = 48$$$. Its runs of consecutive ones have lengths $$$9$$$, $$$4$$$, and $$$3$$$. The corresponding total rewards are $$$32$$$, $$$15$$$, and $$$14$$$, respectively. Therefore, $$$f(a) = 48 + 32 + 15 + 14 = 109$$$.
Moreover, $$$I(a)$$$ consists of $$$18$$$ ones. During the computation of $$$f(I(a))$$$, they form three complete blocks of $$$6$$$ ones, and each block contributes $$$6 \cdot 3 + 5 + 9 + 1 + 3 = 36$$$. Thus, $$$f(I(a)) = 3 \cdot 36 = 108$$$. Since $$$f(a) \gt f(I(a))$$$, the answer is "YES".
In the second test case, no binary array $$$a$$$ satisfies $$$f(a) \gt f(I(a))$$$, so the answer is "NO".
You are given an integer $$$k$$$ and a matrix $$$v$$$ with $$$n$$$ rows and $$$m$$$ columns. The element in row $$$i$$$ and column $$$j$$$ is denoted by $$$v_{i,j}$$$.
A cell $$$(x, y)$$$ is called a peak if its value is greater than or equal to the sum of the values in all other cells in row $$$x$$$ and column $$$y$$$. Formally, $$$(x, y)$$$ is a peak if
$$$$$$ v_{x,y} \ge \sum_{\substack{1 \le i \le n \\i \neq x}} v_{i,y} + \sum_{\substack{1 \le j \le m \\j \neq y}} v_{x,j}. $$$$$$
You may perform the following operation any number of times (possibly zero):
Find the minimum number of operations required to obtain a matrix with at least $$$k$$$ peaks.
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.
The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 10^5$$$, $$$1 \le k \le n \cdot m$$$, $$$n \cdot m \le 10^5$$$) — the number of rows, the number of columns, and the required number of peaks.
The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$v_{i,1}, v_{i,2}, \ldots, v_{i,m}$$$ ($$$-10^9 \le v_{i,j} \le 10^9$$$).
It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a single integer — the minimum number of operations required to obtain at least $$$k$$$ peaks.
If there is no solution, print a single integer $$$-1$$$.
163 3 7-1 -30 76 -3 221 -18 163 1 21000000003000000002000000001 3 12 3 41 3 11 2 31 2 25 75 2 8531959596 -61416172425363565 672913308981527099 451180253-161687136 898803495-388356105 8977233131 1 101 1 1-51 4 3-8 -4 4 -111 9 5-12 -4 -13 9 -1 -15 6 -15 -41 3 3-12 -15 81 5 510 8 -4 0 -41 3 312 9 -11 3 3-14 11 -61 6 6-1 0 15 3 1 -141 2 21000000000 -1000000000
21000000001027345926980-100116121172000000000
For the first test case, you can perform the following two operations:
After these operations, the matrix will be:
| $$$\color{green}{-1}$$$ | $$$-31$$$ | $$$\color{green}{6}$$$ |
| $$$5$$$ | $$$\color{green}{-5}$$$ | $$$\color{green}{20}$$$ |
| $$$\color{green}{0}$$$ | $$$\color{green}{-20}$$$ | $$$\color{green}{14}$$$ |
The seven peaks are highlighted in green. For example:
It can be shown that fewer than two operations cannot create seven peaks, so the answer is $$$2$$$.
In the eighth test case, the only cell has value $$$-5$$$. A cell in a $$$1 \times 1$$$ matrix is a peak if and only if its value is non-negative. Since every operation only decreases its value, it is impossible to make it a peak.
For the last test case, both cells are peaks exactly when their values are equal. Therefore, the first cell must be decreased from $$$10^9$$$ to $$$-10^9$$$, which requires $$$2 \cdot 10^9$$$ operations.
Alisa has a shopping card with an initial balance of $$$h$$$. There are $$$n$$$ types of products in a supermarket. A product of type $$$i$$$ costs $$$c_i$$$, and Alisa can buy any number of products of each type.
There are also $$$m$$$ rebate activities. The $$$j$$$-th activity applies to a purchase if its total cost is at least $$$a_j$$$ and gives a rebate of $$$b_j$$$.
For each purchase, Alisa performs the following operations:
For every $$$h$$$ from $$$1$$$ to $$$s$$$, determine whether Alisa can make the balance on her card equal to $$$0$$$ after finitely many purchases.
The first line contains three integers $$$n$$$, $$$m$$$, and $$$s$$$ ($$$1 \le n, m, s \le 125\,000$$$) — the number of product types, the number of rebate activities, and the maximum initial balance to consider, respectively.
The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 125\,000$$$) — the price of a product of each type.
The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 125\,000$$$) — the spending threshold and the rebate of the $$$i$$$-th activity, respectively.
It is guaranteed that $$$a_1 \lt a_2 \lt \cdots \lt a_m$$$ and $$$b_1 \lt b_2 \lt \cdots \lt b_m$$$.
Print $$$s$$$ lines. For each $$$i$$$ ($$$1 \le i \le s$$$), print "YES" if Alisa can make the balance on her card equal to $$$0$$$ when its initial balance is $$$i$$$, and print "NO" otherwise.
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.
2 1 154 78 4
NO NO NO YES NO NO YES YES NO NO YES YES NO YES YES
5 3 254 8 12 16 206 87 98 10
NO NO NO YES NO NO NO YES NO YES NO YES NO YES NO YES NO YES NO YES NO YES NO YES NO
In the first example, Alisa can make the balance equal to $$$0$$$ when the initial balance is $$$15$$$ as follows:
Therefore, the $$$15$$$-th line of the first example output is "YES".
In the second example, if the initial balance is $$$4$$$, Alisa can buy one product of the first type for $$$4$$$. Since $$$4 \lt 6$$$, no rebate activity applies, and the balance becomes $$$0$$$. Therefore, the fourth line of the second example output is "YES". If the initial balance is $$$1$$$, no product is affordable, so the first line is "NO".