An array is called balanced if every integer $$$x$$$ that occurs at least once, occurs exactly $$$x$$$ times in the array. For example, $$$[1, 4, 2, 4, 4, 4, 2]$$$ is balanced, but $$$[2]$$$ and $$$[2, 2, 2]$$$ are not.
You are given an array $$$a$$$ of $$$n$$$ elements, $$$[a_1, a_2, \ldots, a_n]$$$. The array may not be balanced currently, and you can delete some elements to make it balanced. What is the minimum number of elements you need to delete to make the array balanced?
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 an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the size of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$) — the elements of the array $$$a$$$.
Note that there are no constraints on the sum of $$$n$$$ over all test cases.
For each test case, output a single line containing an integer: the minimum number of elements you should remove from the array to make it balanced.
431 2 251 1 2 2 3101 2 3 2 4 4 4 4 5 210
0231
In the first test case, the given array is already balanced.
In the second test case, we can delete one occurrence of $$$1$$$ and one occurrence of $$$3$$$ to get the array $$$[1, 2, 2]$$$ which is balanced.
You are analyzing an infinite grid with coordinates $$$(X, Y)$$$ (specifically, the cell immediately above $$$(0, 0)$$$ is $$$(0, 1)$$$, and the cell immediately on the right of $$$(0, 0)$$$ is $$$(1, 0)$$$). Initially, only the cell at $$$(0, 0)$$$ is black.
You are given a string $$$a_1a_2 \ldots a_n$$$ of length $$$n$$$ consisting of characters $$$\texttt{"4"}$$$ and $$$\texttt{"8"}$$$, which describes $$$n$$$ expansion operations. For each $$$i$$$ from $$$1$$$ to $$$n$$$, the following happens for all cells simultaneously:
Is the cell $$$(x, y)$$$ black at the end of the process?
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$$$, $$$x$$$, $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5, -10^9 \le x, y \le 10^9$$$) — the number of expansion operations, and the $$$x$$$ and $$$y$$$ coordinates of the cell you are interested in, respectively.
The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$\texttt{"4"}$$$ and $$$\texttt{"8"}$$$ — the types of expansion operations.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output $$$\texttt{YES}$$$ if the cell $$$(x, y)$$$ is black after the expansion operations described by the string $$$s$$$, and $$$\texttt{NO}$$$ otherwise.
The judge is case-insensitive (for example, $$$\texttt{YES}$$$, $$$\texttt{Yes}$$$, $$$\texttt{yes}$$$, $$$\texttt{yEs}$$$ will all be recognized as positive answers).
63 3 38884 5 148844 3 -348847 -7 -5488488410 0 048848848881 1 14
YESNOYESNOYESNO
The first three test cases are illustrated below:
In the first test case, after the expansion operations in the string $$$\texttt{"888"}$$$, cell $$$(3, 3)$$$ is black, so the answer is $$$\texttt{YES}$$$.
In the second test case, cell $$$(5, 1)$$$ is white after the expansion operations in the string $$$\texttt{"4884"}$$$.
You are given three positive integers $$$n$$$, $$$k$$$, and $$$q$$$. You are also given $$$q$$$ tuples $$$(c, l, r)$$$, with $$$1 \leq c \leq 2$$$ and $$$1 \leq l \leq r \leq n$$$.
An array $$$a_1, a_2, \ldots, a_n$$$ is meximum if $$$0 \leq a_i \leq 10^9$$$ for each $$$i$$$ in $$$[1, n]$$$, and for each given tuple $$$(c, l, r)$$$,
Note that the parameter $$$k$$$ is the same for all the conditions.
Find a meximum array $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$. The input is generated in such a way that a valid array always exists. If there are multiple possible arrays, you can print any one of them.
$$$^{\text{∗}}$$$The minimum excluded (MEX) of a collection of integers $$$a_1, a_2, \ldots, a_k$$$ is defined as the smallest non-negative integer $$$x$$$ which does not occur in the collection $$$a$$$.
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$$$, $$$q$$$ ($$$1 \leq k \leq n \leq 100$$$, $$$1 \leq q \leq 100$$$) — the length of the array $$$a_1, a_2, \ldots, a_n$$$, the result of the $$$\min$$$ and $$$\operatorname{MEX}$$$ calculations described by the tuples, and the number of tuples.
Then, $$$q$$$ lines follow. The $$$i$$$-th line contains a tuple $$$(c, l, r)$$$, which gives a constraint to $$$a_l, a_{l+1}, \ldots, a_r$$$ according to the statement.
It is guaranteed that there exists a valid array corresponding to the given input.
Note that there are no constraints on the sum of $$$n$$$, $$$k$$$, or $$$q$$$ over all test cases.
For each test case, print a single line containing a meximum array $$$a_1, a_2, \ldots, a_n$$$.
46 2 21 1 32 2 63 3 12 1 33 3 21 1 11 3 33 2 22 1 22 2 3
2 5 4 3 0 1 2 0 1 3 3 3 1 0 1
In the first test case, you have to build a meximum array with $$$n = 6$$$, $$$k = 2$$$ and with $$$q = 2$$$ constraints given by tuples.
A possible array that satisfies all these constraints, and has elements $$$0 \leq a_i \leq 10^9$$$, is $$$[2, 5, 4, 3, 0, 1]$$$.
In the second test case, you have to build a meximum array with $$$n = 3$$$, $$$k = 3$$$ and with $$$q = 1$$$ constraint given by a tuple.
A possible array that satisfies this constraint, and has elements $$$0 \leq a_i \leq 10^9$$$, is $$$[2, 0, 1]$$$.
You are following the world championship of the Billion Players Game. There are $$$10^9$$$ players competing, and you want to predict the final ranking $$$p$$$ of Godflex, your favorite streamer. After analyzing recent matches, you are sure that $$$l \leq p \leq r$$$, but you don't know anything else.
There are $$$n$$$ offers made by the in-game bookmaker. In the $$$i$$$-th offer, the bookmaker suggests an estimation $$$a_i$$$ for Godflex's final ranking. For each offer, you must choose exactly one of the following actions:
After you decide how to deal with all the offers, the actual Billion Players Game is played. Godflex gets a position $$$p$$$ in $$$[l, r]$$$, and then all the offers are settled up.
Your total score is the number of robocoins you are guaranteed to earn, that is, the minimum number of robocoins you earn over all possible values of $$$p$$$ in $$$[l, r]$$$. Find the maximum possible score you can guarantee.
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$$$, $$$l$$$, $$$r$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq l \leq r \leq 10^9$$$) — the number of offers and the possible range of Godflex's final ranking.
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 bookmaker's estimations of Godflex's ranking in each offer.
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 line containing an integer: the maximum possible score you can guarantee.
41 1 532 100 10050 2005 1 105 7 3 9 15 6 109 3 1 7 5
01501213
In the first test case, there is only one offer.
So the maximum possible score is $$$0$$$.
In the second test case, it is optimal to accept the offers claiming that $$$p \geq 50$$$ and $$$p \leq 200$$$. Since $$$p$$$ must be $$$100$$$, you gain $$$|100-50| + |100-200| = 150$$$ robocoins.
You are managing a fleet of $$$n$$$ drones, each with an energy level $$$a_1, \ldots, a_n$$$. You are also given a positive integer $$$k$$$, which represents the maximum number of drones allowed to share the same energy level.
To prevent overloads, the drones automatically perform energy balancing operations as follows. While there exists an energy level that appears strictly more than $$$k$$$ times, they execute the following steps:
The process stops once no energy level appears more than $$$k$$$ times. Determine how many energy balancing operations will be performed.
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$$$, $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of drones and the maximum allowed number of drones with the same energy level.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2n$$$) — the initial energy levels of the drones.
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 line containing an integer: the number of energy balancing operations performed.
56 31 1 1 1 1 15 11 3 2 1 46 21 1 1 2 3 34 18 8 8 82 21 2
34430
In the first test case, the drones' energy levels evolve as follows:
After $$$3$$$ operations, every energy level appears at most $$$3$$$ times, so the process stops.
In the second test case, the energy levels change as follows: $$$[1, 3, 2, 1, 4] \rightarrow [1, 3, 2, 2, 4] \rightarrow [1, 3, 2, 3, 4] \rightarrow [1, 3, 2, 4, 4] \rightarrow [1, 3, 2, 4, 5]$$$. The process stops after $$$4$$$ operations.
You are an adventurer working for the futuristic corporation RoboCorp. Your current skill level is an integer $$$s$$$ in the range $$$[1, n]$$$, but you don't know its exact value. Your goal is to reach skill $$$\geq n$$$ by completing missions for RoboCorp, but each mission costs precious robocoins.
You can choose missions of any difficulty and any positive integer duration (in hours). However, the cost depends both on the new mission's length, and on how the new mission's difficulty compares to your most recent one.
Let $$$x$$$ be the difficulty of your most recent mission. If you want to undertake a new mission of difficulty $$$y$$$ and duration $$$l$$$:
Your skill improves only when you take on a mission whose difficulty exactly matches your current skill:
After each mission, you still don't know your actual skill value.
You start with $$$10^6$$$ robocoins. Find a strategy (a sequence of missions) that does not exceed your budget and guarantees your skill becomes at least $$$n$$$, no matter what your initial skill was.
The input contains a single line with an integer $$$n$$$ ($$$n = 4$$$ or $$$n = 250\,000$$$) — the target skill level (that is, by the end, your skill must be $$$\geq n$$$).
There are exactly $$$2$$$ tests in this problem (including the example). The example has $$$n = 4$$$, and the other test has $$$n = 250\,000$$$.
Hacks are not allowed in this problem.
In the first line, print a single integer $$$k$$$ ($$$0 \leq k \leq 10^6$$$) — the number of missions you plan to complete.
Then, print $$$k$$$ lines. The $$$i$$$-th line must contain two integers $$$y$$$ and $$$l$$$ ($$$1 \leq y, l \leq 10^6$$$) — the difficulty and duration (in hours) of the $$$i$$$-th mission, respectively.
4
4 1 4 3 1 2 1 3 1
In the example, your target skill is $$$n = 4$$$. You can use the following strategy:
In total, you spend $$$2007$$$ robocoins, which is within your $$$10^6$$$-robocoin budget.
Now we verify that this strategy always works:
So no matter what your starting skill is, you end with skill $$$\geq n$$$.
You have reached the final level of the popular roguelike game "Isaac's Keybindings". Instead of a boss, you encounter a shopkeeper who holds an hidden array of integers $$$a_1, a_2, \ldots, a_n$$$, where $$$0 \leq a_i \lt 2^{30}$$$ for each $$$i$$$ in $$$[1, n]$$$.
It is guaranteed that the array is generated randomly, i.e., each $$$a_i$$$ ($$$1 \leq i \leq n$$$) is an integer independently generated uniformly at random in $$$[0, 2^{30})$$$, in all the tests excluding the example.
Let $$$f(u, v) = a_u \oplus a_{u+1} \oplus \ldots \oplus a_v$$$, where $$$\oplus$$$ is the bitwise $$$\texttt{XOR}$$$.
You can ask queries of the following form: $$$\texttt{? u v}$$$, with $$$1 \leq u \leq v \leq n$$$.
The answer to the query is:
Each query has a cost of $$$\frac{1}{v-u+1}$$$ robocoins. On each test, you are given $$$300$$$ robocoins in total to pass at most $$$30$$$ test cases (that is, you can spend $$$10$$$ robocoins on average for a single test case). If your balance ever becomes negative you lose. Note that your robocoin balance does not need to be an integer at any moment.
Find the answer to all possible $$$\frac{n(n+1)}{2}$$$ queries without losing.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 30$$$). The description of the test cases follows.
The first line of each test case contains one integer $$$n$$$ ($$$n = 3$$$ or $$$n = 100$$$) — the length of the array $$$a_1, a_2, \ldots, a_n$$$. It is guaranteed that the array is generated randomly in all the tests excluding the example.
There are exactly $$$50$$$ tests in this problem (including the example). The example has $$$t = 1$$$ and $$$n = 3$$$, and all the other tests have $$$t = 30$$$ and $$$n = 100$$$.
Hacks are not allowed in this problem.
For each test case, first read a single integer $$$n$$$. If the integer you read is $$$-2$$$, it means that the answer to the previous test case was wrong, and you should exit immediately.
To ask a query, print a line in the format $$$\texttt{? u v}$$$ with $$$1 \leq u \leq v \leq n$$$.
As a response to the query, you will receive $$$-2$$$ if you made an invalid query (i.e., the format is invalid, or you would reach a negative amount of robocoins). In that case, you should exit immediately. Otherwise, you will receive the answer to the query.
When you determine the answers to all the $$$\frac{n(n+1)}{2}$$$ queries, print them in the following format.
Print $$$n+1$$$ lines. The first line must contain a single character $$$\texttt{!}$$$. The $$$i$$$-th of the next $$$n$$$ lines must contain $$$n-i+1$$$ integers: the answers to queries $$$\texttt{? i i}, \, \texttt{? i i+1}, \,\ldots, \, \texttt{? i n}$$$, respectively.
After printing a query, do not forget to output the end of line and flush the output. Otherwise, you will get $$$\texttt{Idleness limit exceeded}$$$. To do this, use:
1 3 2 -1 1
? 1 2 ? 1 3 ? 2 3 ! 1 2 -1 2 1 2
In the example, the hidden array is $$$[a_1, a_2, a_3] = [2, 4, 6]$$$.
Now, even without knowing the hidden array, you claim the answer to all the possible $$$6$$$ queries. For example, you claim that the answer to $$$\texttt{? 1 1}$$$ is $$$1$$$.
You have spent $$$1/2 + 1/3 + 1/2 = 4/3$$$ robocoins, which is less than the allowed $$$300$$$ robocoins.
A permutation$$$^{\text{∗}}$$$ of length $$$n$$$ is called valid if it has both of the following properties:
Let $$$k$$$ denote the number of permutations satisfying the above condition. Your task is to find and print $$$\min(k, 2000)$$$ examples of such permutations.
$$$^{\dagger}$$$ A permutation $$$p_1, p_2, \ldots, p_n$$$ is bitonic if there exists an index $$$i$$$ ($$$1 \leq i \leq n$$$) such that
$$$^{\ddagger}$$$ A subset $$$C \subseteq \{ 1, 2, \ldots, n \}$$$ is a cycle if it satisfies the following conditions:
$$$^{\text{∗}}$$$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).
The input consists of a single line containing two integers $$$n$$$, $$$m$$$ ($$$1 \le m \leq n \le 100$$$) — the length of the permutations and the target number of cycles.
In the first line, print a single integer $$$r$$$: the number of permutations you are going to print. Note that $$$r$$$ must be $$$\min(k, 2000)$$$ as defined in the statement.
Then, print $$$r$$$ lines. Each line must contain a bitonic permutation of length $$$n$$$, with $$$m$$$ cycles.
6 3
9 1 4 5 6 3 2 6 5 4 3 2 1 1 2 4 5 6 3 1 2 5 6 4 3 1 3 4 6 5 2 1 5 6 4 3 2 3 5 6 4 2 1 1 3 6 5 4 2 2 6 5 4 3 1
In the example, there are $$$9$$$ valid permutations (i.e., bitonic permutations of length $$$6$$$, with $$$3$$$ cycles). For example, $$$[3, 5, 6, 4, 2, 1]$$$ is bitonic (in the above definition, $$$i = 3$$$), and it has $$$3$$$ cycles: $$$\{1, 3, 6\}$$$, $$$\{2, 5\}$$$, $$$\{4\}$$$. So you have to print $$$r = \min(9, 2000) = 9$$$ such permutations.
You and Bob are playing Hyper Smawk Bros against each other, facing a single boss with health $$$n$$$.
You and Bob act alternately, and you start. On your turn, you may use an attack that deals an integer amount of damage $$$x$$$ in $$$[1, m]$$$, replacing $$$n$$$ with $$$n - x$$$. However, one cannot use the same $$$x$$$ that his opponent just used on the previous turn (on the very first move of the game, any $$$x$$$ in $$$[1, m]$$$ is allowed).
The winner is the first player to reduce the boss's health to $$$n \leq 0$$$. Determine whether you can force a win if Bob plays optimally.
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 only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 10^6$$$, $$$2 \leq m \leq 10^6$$$) — the starting health $$$n$$$ and the maximum damage per attack $$$m$$$.
Note that there are no constraints on the sum of $$$n$$$ over all test cases, and there are no constraints on the sum of $$$m$$$ over all test cases.
For each test case, output $$$\texttt{YES}$$$ if you can force a win against Bob, and $$$\texttt{NO}$$$ otherwise.
The judge is case-insensitive (for example, $$$\texttt{YES}$$$, $$$\texttt{Yes}$$$, $$$\texttt{yes}$$$, $$$\texttt{yEs}$$$ will all be recognized as positive answers).
86 920 1069 242 942 1044 944 10400000 400000
YESYESNONOYESYESNOYES
In the first test case, you can win immediately by dealing damage $$$8$$$, so that $$$n$$$ becomes $$$6-8 = -2 \leq 0$$$.
In the second test case,
In the third test case,
In both cases, you lose.