Nene invented a new game based on an increasing sequence of integers $$$a_1, a_2, \ldots, a_k$$$.
In this game, initially $$$n$$$ players are lined up in a row. In each of the rounds of this game, the following happens:
Once no one is kicked out of the game in some round, all the players that are still in the game are declared as winners.
For example, consider the game with $$$a=[3, 5]$$$ and $$$n=5$$$ players. Let the players be named player A, player B, $$$\ldots$$$, player E in the order they are lined up initially. Then,
Nene has not yet decided how many people would join the game initially. Nene gave you $$$q$$$ integers $$$n_1, n_2, \ldots, n_q$$$ and you should answer the following question for each $$$1 \le i \le q$$$ independently:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 250$$$). The description of test cases follows.
The first line case contains two integers $$$k$$$ and $$$q$$$ ($$$1 \le k, q \le 100$$$) — the length of the sequence $$$a$$$ and the number of values $$$n_i$$$ you should solve this problem for.
The second line contains $$$k$$$ integers $$$a_1,a_2,\ldots,a_k$$$ ($$$1\leq a_1 \lt a_2 \lt \ldots \lt a_k\leq 100$$$) — the sequence $$$a$$$.
The third line contains $$$q$$$ integers $$$n_1,n_2,\ldots,n_q$$$ ($$$1\leq n_i \leq 100$$$).
For each test case, output $$$q$$$ integers: the $$$i$$$-th ($$$1\le i \le q$$$) of them should be the number of players declared as winners if initially $$$n_i$$$ players join the game.
62 13 555 32 4 6 7 91 3 55 43 4 5 6 71 2 3 42 369 961 10 1001 1100503 310 20 301 10 100
21 1 11 2 2 21 10 68501 9 9
The first test case was explained in the statement.
In the second test case, when $$$n=1$$$, the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner.
You and Nene are playing a card game. The deck with $$$2n$$$ cards is used to play this game. Each card has an integer from $$$1$$$ to $$$n$$$ on it, and each of integers $$$1$$$ through $$$n$$$ appears exactly on $$$2$$$ cards. Additionally, there is a table where cards are placed during the game (initially, the table is empty).
In the beginning of the game, these $$$2n$$$ cards are distributed between you and Nene so that each player receives $$$n$$$ cards.
After it, you and Nene alternatively take $$$2n$$$ turns, i.e. each person takes $$$n$$$ turns, starting with you. On each turn:
Note that turns are made publicly: each player can see all the cards on the table at each moment.
Nene is very smart so she always selects cards optimally in order to maximize her score in the end of the game (after $$$2n$$$ rounds). If she has several optimal moves, she selects the move that minimizes your score in the end of the game.
More formally, Nene always takes turns optimally in order to maximize her score in the end of the game in the first place and to minimize your score in the end of the game in the second place.
Assuming that the cards are already distributed and cards in your hand have integers $$$a_1, a_2, \ldots, a_n$$$ written on them, what is the maximum number of points you can get by taking your turns 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 test cases follows.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of cards you and Nene receive in the beginning of the game.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the integers on the cards in your hand. It is guaranteed that each integer from $$$1$$$ through $$$n$$$ appears in the sequence $$$a_1, a_2, \ldots, a_n$$$ at most $$$2$$$ times.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one integer: the maximum number of points you can get.
541 1 2 387 4 1 2 8 8 5 587 1 4 5 3 4 2 631 2 311
12100
In the first test case, the integers written on your cards are $$$1$$$, $$$1$$$, $$$2$$$ and $$$3$$$. The integers written on Nene's cards are $$$2$$$, $$$3$$$, $$$4$$$ and $$$4$$$. The game may proceed as follows:
At the end of the game, you scored $$$1$$$ point, and Nene scored $$$3$$$. It can be shown that you cannot score more than $$$1$$$ point if Nene plays optimally, so the answer is $$$1$$$.
In the second test case, if both players play optimally, you score $$$2$$$ points and Nene scores $$$6$$$ points.
The magical girl Nene has an $$$n\times n$$$ matrix $$$a$$$ filled with zeroes. The $$$j$$$-th element of the $$$i$$$-th row of matrix $$$a$$$ is denoted as $$$a_{i, j}$$$.
She can perform operations of the following two types with this matrix:
Nene wants to maximize the sum of all the numbers in the matrix $$$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}a_{i,j}$$$. She asks you to find the way to perform the operations so that this sum is maximized. As she doesn't want to make too many operations, you should provide a solution with no more than $$$2n$$$ operations.
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).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of test cases follows.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 500$$$) — the size of the matrix $$$a$$$.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.
For each test case, in the first line output two integers $$$s$$$ and $$$m$$$ ($$$0\leq m\leq 2n$$$) — the maximum sum of the numbers in the matrix and the number of operations in your solution.
In the $$$k$$$-th of the next $$$m$$$ lines output the description of the $$$k$$$-th operation:
Note that you don't need to minimize the number of operations used, you only should use no more than $$$2n$$$ operations. It can be shown that the maximum possible sum can always be obtained in no more than $$$2n$$$ operations.
212
1 11 1 17 31 1 1 21 2 1 22 1 1 2
In the first test case, the maximum sum $$$s=1$$$ can be obtained in $$$1$$$ operation by setting $$$a_{1, 1}:=1$$$.
In the second test case, the maximum sum $$$s=7$$$ can be obtained in $$$3$$$ operations as follows:
It can be shown that it is impossible to make the sum of the numbers in the matrix larger than $$$7$$$.
Nene gave you an array of integers $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$.
You can perform the following operation no more than $$$5\cdot 10^5$$$ times (possibly zero):
Here, $$$\operatorname{MEX}$$$ of a set of integers $$$\{c_1, c_2, \ldots, c_k\}$$$ is defined as the smallest non-negative integer $$$m$$$ which does not occur in the set $$$c$$$.
Your goal is to maximize the sum of the elements of the array $$$a$$$. Find the maximum sum and construct a sequence of operations that achieves this sum. Note that you don't need to minimize the number of operations in this sequence, you only should use no more than $$$5\cdot 10^5$$$ operations in your solution.
The first line contains an integer $$$n$$$ ($$$1 \le n \le 18$$$) — the length of the array $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\leq a_i \leq 10^7$$$) — the array $$$a$$$.
In the first line, output two integers $$$s$$$ and $$$m$$$ ($$$0\le m\le 5\cdot 10^5$$$) — the maximum sum of elements of the array $$$a$$$ and the number of operations in your solution.
In the $$$i$$$-th of the following $$$m$$$ lines, output two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), representing the parameters of the $$$i$$$-th operation.
It can be shown that the maximum sum of elements of the array $$$a$$$ can always be obtained in no more than $$$5 \cdot 10^5$$$ operations.
20 1
4 1 1 2
31 3 9
13 0
41 100 2 1
105 2 3 3 3 4
10
1 1 1 1
In the first example, after the operation with $$$l=1$$$ and $$$r=2$$$ the array $$$a$$$ becomes equal to $$$[2,2]$$$. It can be shown that it is impossible to achieve a larger sum of the elements of $$$a$$$, so the answer is $$$4$$$.
In the second example, the initial sum of elements is $$$13$$$ which can be shown to be the largest.
In the third example, the array $$$a$$$ changes as follows:
It can be shown that it is impossible to achieve a larger sum of the elements of $$$a$$$, so the answer is $$$105$$$.
This is the easy version of the problem. The only difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if both versions of the problem are solved.
Nene is fighting with $$$n$$$ monsters, located in a circle. These monsters are numbered from $$$1$$$ to $$$n$$$, and the $$$i$$$-th ($$$1 \le i \le n$$$) monster's current energy level is $$$a_i$$$.
Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one:
When the monster with energy level $$$x$$$ attacks the monster with the energy level $$$y$$$, the energy level of the defending monster becomes $$$\max(0, y-x)$$$ (the energy level of the attacking monster remains equal to $$$x$$$).
Nene is going to use this spell $$$10^{100}$$$ times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell $$$10^{100}$$$ times.
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 test cases follows.
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of monsters.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 2 \cdot 10^5$$$) — the current energy levels of monsters.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case,
If $$$m=0$$$, you may either output an empty line or don't output it.
532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0
1101121 361 3 6 8 10 12
In the first test case, the following actions happen during the first $$$3$$$ uses of the spell in this order:
After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the $$$1$$$-st monster has a non-zero energy level in the end.
In the second test case, both monsters initially have zero energy level.
This is the hard version of the problem. The only difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if both versions of the problem are solved.
Nene is fighting with $$$n$$$ monsters, located in a circle. These monsters are numbered from $$$1$$$ to $$$n$$$, and the $$$i$$$-th ($$$1 \le i \le n$$$) monster's current energy level is $$$a_i$$$.
Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one:
When the monster with energy level $$$x$$$ attacks the monster with the energy level $$$y$$$, the energy level of the defending monster becomes $$$\max(0, y-x)$$$ (the energy level of the attacking monster remains equal to $$$x$$$).
Nene is going to use this spell $$$10^{100}$$$ times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell $$$10^{100}$$$ times.
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 test cases follows.
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of monsters.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the current energy levels of monsters.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case,
If $$$m=0$$$, you may either output an empty line or don't output it.
532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0
1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
In the first test case, the following actions happen during the first $$$3$$$ uses of the spell in this order:
After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the $$$1$$$-st monster has a non-zero energy level in the end.
In the second test case, both monsters initially have zero energy level.
Nene is training her team as a basketball coach. Nene's team consists of $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th player has an arm interval $$$[l_i,r_i]$$$. Two players $$$i$$$ and $$$j$$$ ($$$i \neq j$$$) can pass the ball to each other if and only if $$$|i-j|\in[l_i+l_j,r_i+r_j]$$$ (here, $$$|x|$$$ denotes the absolute value of $$$x$$$).
Nene wants to test the cooperation ability of these players. In order to do this, she will hold several rounds of assessment.
As a coach, Nene wants each of $$$n$$$ players to appear in at least one round of assessment. Since Nene has to go on a date after school, Nene wants you to calculate the minimum number of rounds of assessment needed to complete the task.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^5$$$). The description of test cases follows.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^6$$$) — the number of players.
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$) — the arm interval of the $$$i$$$-th player.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^6$$$.
For each test case, output one integer — the minimum number of rounds of assessment Nene needs to complete her work.
521 11 121 12 231 31 31 351 12 21 52 21 161 25 52 32 32 21 2
22213
In the first two test cases, Nene can host two rounds of assessment: one with $$$p=[1]$$$ and one with $$$p=[2]$$$. It can be shown that hosting one round of assessment is not enough, so the answer is $$$2$$$.
In the third test case, Nene can host two rounds of assessment: one with $$$p=[1,3]$$$ and one with $$$p=[2]$$$. Player $$$1$$$ can pass the ball to player $$$3$$$ as $$$|3-1|=2 \in [1+1,3+3]$$$. It can be shown that hosting one round of assessment is not enough, so the answer is $$$2$$$.